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

标签 swift colors label swift3 nsmutableattributedstring

<分区>

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

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

swift 3

如下图-

Expected result

最佳答案

使用 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

关于swift - 如何更改标签中某些单词的颜色 - Swift 3,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39665423/

相关文章:

ios - 推送搜索结果后,顶部布局指南会移至导航栏下方

ios - swift 4 : Presenting ViewControllers with a UIView as the frame

ios - 我应该使用 KVO 还是 NSNotificationCenter?

c# - 生成指定数量的随机但独特的颜色

java - Antlr4的LABEL_CONFLICTS_WITH_RULE的含义

ios - iOS 应用程序的版本号和内部版本号可以是字符串吗?

linux - 如何创建像 debian 安装程序一样简单的命令行 UI?

java - 使用具有百分比宽度的列时,GridPane 没有为标签提供足够的空间

Swift - 当以编程方式设置字体大小并运行模拟器时,使用 Storyboard的属性文本功能全部重置

python - 使用 plt.colorbar() 时在 matplotlib 中定义自定义标准化函数