7

Hi I'm trying to use addAttribute in Swift3.

I want set bold only IDNAME.

Here is what I am trying to this.

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))

How do I get index of the IDNAME i.e different every time?

-> Is there a way to split space and get index?

Nirav D
  • 71,513
  • 12
  • 161
  • 183
Shawn Baek
  • 1,928
  • 3
  • 20
  • 34

1 Answers1

23

You need to do something like this.

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))
Nirav D
  • 71,513
  • 12
  • 161
  • 183