gpt4 book ai didi

swift - 更改字符串数组中特定文本的颜色。 swift

转载 作者:搜寻专家 更新时间:2023-11-01 06:04:38 25 4
gpt4 key购买 nike

如何更改将要传递到标签的字符串数组中特定文本的颜色?

假设我有一个字符串数组:

var stringData = ["First one", "Please change the color", "don't change me"]

然后它被传递给一些标签:

Label1.text = stringData[0]
Label2.text = stringData[1]
Label3.text = stringData[2]

更改 stringData[1] 中单词“the”颜色的最佳方法是什么?

预先感谢您的帮助!

最佳答案

let str = NSMutableAttributedString(string: "Please change the color")
str.addAttributes([NSForegroundColorAttributeName: UIColor.red], range: NSMakeRange(14, 3))
label.attributedText = str

range 是特定文本的范围。

关于swift - 更改字符串数组中特定文本的颜色。 swift ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41031257/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com