嗨,我正在尝试在
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))