将格式化的字符串附加到来自 UITextView 的已格式化的字符串?
Append formatted string to already formatted string from UITextView?
如何将 NSAttributedString
格式的字符串附加到 UITextView 中现有的 textView.attributedText
格式的字符串?
从 ,我可以看到从 NSMutableAttributedString
/ NSAttributedString
到 NSMutableAttributedString
的转换可以用 appendAttributedString()
/ append()
完成,但是这个 不工作当我像这样拉动和更新textView.attributedText
时:
let string2 = NSAttributedString(string: "success", attributes: [NSAttributedStringKey.foregroundColor: UIColor.green])
let newMutableString = textView.attributedText.copy() as! NSMutableAttributedString
newMutableString.append(string2)
textView.attributedText = newMutableString.copy() as! NSAttributedString
错误信息:
Could not cast value of type 'NSConcreteAttributedString' (0x10c7dff30) to 'NSMutableAttributedString' (0x10c7dff80).
我认为您需要使用 mutableCopy
复制它,因为您想从不可变副本中获取可变副本:
let newMutableString = textView.attributedText.mutableCopy() as! NSMutableAttributedString
如何将 NSAttributedString
格式的字符串附加到 UITextView 中现有的 textView.attributedText
格式的字符串?
从 NSMutableAttributedString
/ NSAttributedString
到 NSMutableAttributedString
的转换可以用 appendAttributedString()
/ append()
完成,但是这个 不工作当我像这样拉动和更新textView.attributedText
时:
let string2 = NSAttributedString(string: "success", attributes: [NSAttributedStringKey.foregroundColor: UIColor.green])
let newMutableString = textView.attributedText.copy() as! NSMutableAttributedString
newMutableString.append(string2)
textView.attributedText = newMutableString.copy() as! NSAttributedString
错误信息:
Could not cast value of type 'NSConcreteAttributedString' (0x10c7dff30) to 'NSMutableAttributedString' (0x10c7dff80).
我认为您需要使用 mutableCopy
复制它,因为您想从不可变副本中获取可变副本:
let newMutableString = textView.attributedText.mutableCopy() as! NSMutableAttributedString