Reputation: 77
My App is presenting some score results and I would like to add a color variation of score labels from red to green according to the result :
self.scoreLabel.text = "\(total100)/100"
What is the easiest way to realize it ?
Thank you in advance
Upvotes: 1
Views: 267
Reputation: 154
Call this function when result change.
func setTextColor(score: Int){
if score < 34 {
self.scoreLabel.textColor = .red
}else {
self.scoreLabel.textColor = .green
}
}
Upvotes: 1
Reputation: 14427
you need to use attributed string
let myMutableString = NSMutableAttributedString(string: "\(total100)/100", attributes: [NSAttributedString.Key.font:UIFont(name: "Georgia", size: 18.0)!])
myMutableString.addAttribute(NSAttributedString.Key.foregroundColor, value: UIColor.red, range: NSRange(location:7,length:3))
self.scoreLabel.attributedText = myMutableString
Upvotes: 2