我有两个NSAttributed字符串但是当我像
那样连接时self.label.text = attrStr1 + attrStr2 //给出错误
var attrStr1: NSAttributedString = NSAttributedString(data: "
<b>Welcome</b>".dataUsingEncoding(NSUnicodeStringEncoding,
allowLossyConversion: true)!,options: [ NSDocumentTypeDocumentAttribute:
NSHTMLTextDocumentType],documentAttributes: nil,error: nil)!
var attrStr2: NSAttributedString = NSAttributedString(data: "
<i>User</i>".dataUsingEncoding(NSUnicodeStringEncoding,
allowLossyConversion: true)!,options: [ NSDocumentTypeDocumentAttribute:
NSHTMLTextDocumentType],documentAttributes: nil,error: nil)!
我们也可以连接一个NSAttributedString和另一个NSString吗?
答案 0 :(得分:1)
var attrStr1: NSAttributedString = NSAttributedString(data: "
<b>Welcome</b>".dataUsingEncoding(NSUnicodeStringEncoding,
allowLossyConversion: true)!,options: [ NSDocumentTypeDocumentAttribute:
NSHTMLTextDocumentType],documentAttributes: nil,error: nil)!
var attrStr2: NSAttributedString = NSAttributedString(data: "
<i>User</i>".dataUsingEncoding(NSUnicodeStringEncoding,
allowLossyConversion: true)!,options: [ NSDocumentTypeDocumentAttribute:
NSHTMLTextDocumentType],documentAttributes: nil,error: nil)!
连接这个字符串:
var concate = NSMutableAttributedString(attributedString: attrStr1)
concate.appendAttributedString(attrStr2)
self.label.text = concate
答案 1 :(得分:1)
2020年| SWIFT 5.1: 您可以像这样添加2个NSAttributedString:
let concatenated = NSAttrStr1.append(NSAttrStr2)
在同一问题上,还有一个更好的答案: https://stackoverflow.com/a/60329354/4423545