如何在swift 3中使用addAttribute和NSRANGE

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

相关文章

软件简介:蓝湖辅助工具,减少移动端开发中控件属性的复制和粘...
现实生活中,我们听到的声音都是时间连续的,我们称为这种信...
前言最近在B站上看到一个漂亮的仙女姐姐跳舞视频,循环看了亿...
【Android App】实战项目之仿抖音的短视频分享App(附源码和...
前言这一篇博客应该是我花时间最多的一次了,从2022年1月底至...
因为我既对接过session、cookie,也对接过JWT,今年因为工作...