1
let myString = text1 + " (\(text2) people added)" let myAttribute = [ NSAttributedStringKey.foregroundColor: UIColor.blue ] let myAttrString = NSAttributedString(string: myString, attributes: myAttribute) cell.textLabel?.attributedText = myAttrString 

How can I use different styles of text1 and " (\(text2) people added)" in one attributed string?

1
  • You can append NSAttributedString using the mutable version NSMutableAttributedString. Commented Jan 26, 2018 at 9:04

1 Answer 1

4

Yes, you can

 var attributedText = NSMutableAttributedString() let str1 = text1 let str2 = " (\(text2) people added)" let attr1 = [NSAttributedStringKey.foregroundColor: UIColor.blue] let attr2 = [NSAttributedStringKey.foregroundColor: UIColor.red] attributedText.append(NSAttributedString(string: str1, attributes: attr1)) attributedText.append(NSAttributedString(string: str2, attributes: attr2)) cell.textLabel?.attributedText = attributedText 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.