【发布时间】:2016-11-03 07:09:45
【问题描述】:
我尝试更改UILabel 部分的文本颜色,如下所示。
func changetheColor () {
let staticStrng : String = "Zave receipt"
var mymutableAttributeString = NSMutableAttributedString()
mymutableAttributeString = NSMutableAttributedString(string: staticStrng)
mymutableAttributeString.addAttribute(NSForegroundColorAttributeName, value: UIColor.redColor(), range: NSRange(location: 5,length: 7))
saveReceiptLabel.text = mymutableAttributeString.string
}
这不起作用,我做错了什么。希望您的帮助。
【问题讨论】:
-
将最后一行替换为
saveReceiptLabel.attributedText = mymutableAttributeString。具有颜色(以及可能的其他渲染效果)和字符串的是 NSAttributedString,而不是只是一个字符链的 (NS)String。 -
我试过了,然后报错
cannot assign value of type NSMutableAttributedString of type String -
saveReceiptLabel.attributedText =,而不是saveReceiptLabel.text =,这是您对我的建议的尝试吗? -
是的,明白了。那是我错过了。谢谢
标签: ios uilabel swift3 textcolor nsmutableattributedstring