您好我正在尝试在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, value: UIFont(name: "SFUIText-Bold", size: 14)!, range: NSRange(location: 0, length: 5))
我如何获得IDNAME的索引,即每次都不同?
- >有没有办法分割空间并获得索引?
答案 0 :(得分:20)
你需要做这样的事情。
let personName = "Black"
let wholeStr = "\(personName) hi nice 2 meet you :D #HELLOW"
let boldUsername = NSMutableAttributedString(string: wholeStr)
boldUsername.addAttribute(NSFontAttributeName, value: UIFont(name: "SFUIText-Bold", size: 14)!, range: (wholeStr as NSString).range(of: personName))