Ios UILabel文本的多文本颜色在swift 3.0中不起作用
我试图更改Ios UILabel文本的多文本颜色在swift 3.0中不起作用,ios,uilabel,swift3,textcolor,nsmutableattributedstring,Ios,Uilabel,Swift3,Textcolor,Nsmutableattributedstring,我试图更改UILabel部分的文本颜色,如下所示 func changetheColor () { let staticStrng : String = "Zave receipt" var mymutableAttributeString = NSMutableAttributedString() mymutableAttributeString = NSMutableAttributedString(string: staticStrng)
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.string
更改此项:
saveReceiptLabel.attributedText = mymutableAttributeString.string
将最后一行替换为
saveReceiptLabel.AttributeText=mymutableAttributeString
。具有颜色(以及可能的其他渲染效果)和字符串的是NSAttributedString,而不是仅仅是一个字符链的(NS)字符串。我尝试了,然后它给出了一个错误无法分配类型为string的NSMutableAttributedString的值类型为string
saveReceiptLabel.attributedText=
,不是saveReceiptLabel.text=
,这就是你尝试过的我的建议吗?是的,明白了。那是我错过的。然后将最后一行替换为saveReceiptLabel.AttributeText=mymutableAttributeString
。具有颜色(以及可能的其他渲染效果)和字符串的是NSAttributedString,而不是仅仅是一个字符链的(NS)字符串。我尝试了,然后它给出了一个错误无法分配类型为string的NSMutableAttributedString的值类型为string
saveReceiptLabel.attributedText=
,不是saveReceiptLabel.text=
,这就是你尝试过的我的建议吗?是的,明白了。那是我错过的。thanx@AnuradhS:这个代码真的有效吗?因为最后,它是mymutableAttributeString.string
而不是mymutableAttributeString
?@Larme是的,我已经使用了它,没有AttributeText
部分。在swift 3中,我们只使用mymutableAttributeString
它会给您一个错误,名为“无法分配字符串类型NSMutableAttributeString的值”@AnuradhS我不使用swift,但对我来说,它要么是myLabel.text=MyAttributeString.String
要么是myLabel.AttributeText=MyAttributeString
,这不会有同样的效果,但你不应该混合它们。@Larme它对我来说很好,如果我不使用。string
它会给我一个错误。奇怪。不应该saveReceiptLabel.attributedText=mymutableAttributeString.string
=>“无法将“string”类型的值分配给“NSAttributeString”类型,saveReceiptLabel.text=mymutableAttributeString
=>“无法将“NSMutableAttributeString”类型的值分配给“string”类型”,这很有意义。@AnuradhS:这段代码真的有效吗?因为最后它是mymutableAttributeString.string
而不是mymutableAttributeString
?@Larme是的,我已经在没有AttributeText
部分的情况下使用了它。在swift 3中,我们只使用mymutableAttributeString
它会给您一个错误,名为“无法分配字符串类型NSMutableAttributeString的值”@AnuradhS我不使用swift,但对我来说,它要么是myLabel.text=MyAttributeString.String
要么是myLabel.AttributeText=MyAttributeString
,这不会有同样的效果,但你不应该混合它们。@Larme它对我来说很好,如果我不使用。string
它会给我一个错误。奇怪。不应该saveReceiptLabel.attributedText=mymutableAttributeString.string
=>“无法将'string'类型的值分配给'NSAttributeString'”类型,saveReceiptLabel.text=mymutableAttributeString
=>“无法将'NSMutableAttributeString'类型的值分配给'string'”,这是有意义的。