我有两个 NSAttributedString:
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)!
当我像这样连接它们时:
self.label.text = attrStr1 + attrStr2
我收到错误。
如何连接两个 NSAttributedString?
是否可以连接 NSAttributedString 和 NSString?
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
2020 |环球银行金融电信协会 5.1: 您可以添加 2 个 NSAttributedString,如下所示:
let concatenated = NSAttrStr1.append(NSAttrStr2)
同样的问题还有另一个更好的答案: https://stackoverflow.com/a/60329354/4423545