如何更改标签中某些单词的颜色-Swift 3

Eug*_* Or 1 label colors nsmutableattributedstring swift swift3

用简单的文本创建了一个标签。

如何更改标签中某些单词的颜色?

迅捷3

像下面一样

预期结果

San*_*osh 6

用于NSMutableAttributedString为字符串中的不同单词应用颜色。试试看,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
Run Code Online (Sandbox Code Playgroud)