加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

如何在swift 3中使用addAttribute和NSRANGE

发布时间:2020-12-14 05:26:03 所属栏目:百科 来源:网络整理
导读:嗨,我正在尝试在 Swift3中使用addAttribute. 我想要只设置粗体IDNAME. 这就是我想要的. let boldUsername = NSMutableAttributedString(string: "IDNAME hi nice 2 meet you :D #HELLOW")// boldUsername.addAttribute(NSFontAttributeName,value: UIFont(na
嗨,我正在尝试在 Swift3中使用addAttribute.

我想要只设置粗体IDNAME.

这就是我想要的.

let boldUsername = NSMutableAttributedString(string: "IDNAME hi nice 2 meet you :D #HELLOW")


//            boldUsername.addAttribute(NSFontAttributeName,value: UIFont(name: "SFUIText-Bold",size: 14)!,range: (boldUsername.string as NSString).range(of: " "))

            boldUsername.addAttribute(NSFontAttributeName,range: NSRange(location: 0,length: 5))

我如何获得IDNAME的索引,即每次都不同?

– >有没有办法分割空间并获得索引?

你需要做这样的事情.
let personName = "Black"
let wholeStr = "(personName) hi nice 2 meet you :D #HELLOW"
let boldUsername = NSMutableAttributedString(string: wholeStr)
boldUsername.addAttribute(NSFontAttributeName,range: (wholeStr as NSString).range(of: personName))

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读