gpt4 book ai didi

ios - 更改 UILabel 属性字符串

转载 作者:搜寻专家 更新时间:2023-10-30 23:08:10 24 4
gpt4 key购买 nike

我使用此代码将属性字符串添加到标签:

let attrString = NSMutableAttributedString(string: text)
// add some attributes
myLabel.attributedText = attrString

现在是否可以仅更改属性字符串 myLabel 的文本并保留属性?

最佳答案

通过它的mutableString属性

例子:

let astrMessage = NSMutableAttributedString(string: "Hello World")

//set some attributes
astrMessage.addAttribute(NSAttributedStringKey.foregroundColor,
value: UIColor.blue,
range: NSRange(location: 0, length: 5))
astrMessage.addAttribute(NSAttributedStringKey.foregroundColor,
value: UIColor.red,
range: NSRange(location: 6, length: 5))

//update
astrMessage.mutableString.setString("World Welcome")

注意:只有第一个属性会应用到更新后的文本。

关于ios - 更改 UILabel 属性字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45798121/

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