如何更改标签中某些单词的颜色 - Swift 3 [英] How to change colour of the certain words in label - Swift 3

查看:29
本文介绍了如何更改标签中某些单词的颜色 - Swift 3的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

创建一个带有简单文本的标签.

Created a Label with a simple text.

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

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

斯威夫特 3

如下图-

推荐答案

使用 NSMutableAttributedString 为字符串中的不同单词应用颜色.试试这个,已经有很多类似的问题在SO中有答案.

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

这篇关于如何更改标签中某些单词的颜色 - Swift 3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆