Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change colour of the certain words in label - Swift 3 [duplicate]

Created a Label with a simple text.

How can I change the color of the certain words in the label?

Swift 3

Like below-

Expected result

like image 561
Eugene Or Avatar asked Oct 27 '25 12:10

Eugene Or


1 Answers

Use NSMutableAttributedString to apply colors for different words in a string. Try this, there are already lot of similar questions have the answers in SO.

let myLabel = UILabel()
var stringOne = "Swift 3 has come"
let stringTwo = "has"

let range = (stringOne as NSString).range(of: stringTwo)

let attributedText = NSMutableAttributedString.init(string: stringOne)
attributedText.addAttribute(NSForegroundColorAttributeName, value: UIColor.blue , range: range)
myLabel.attributedText = attributedText
like image 185
Santosh Avatar answered Oct 29 '25 01:10

Santosh



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!