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

喜夏-厌秋 提交于 2019-11-29 15:49:57

问题


Created a Label with a simple text.

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

Swift 3

Like below-


回答1:


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


来源:https://stackoverflow.com/questions/39665423/how-to-change-colour-of-the-certain-words-in-label-swift-3

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!