将NSAttributed文本追加到UITextview [英] Append NSAttributed text to UITextview
问题描述
我不敢相信我在问这个问题,但是(一直在寻找一个半小时而没有运气的答案)... 如何将NSAttributedText附加到UITextView?(在Swift 2.0+中)
I can't believe I am asking this question but (been looking for the answer for an hour and a half with no luck)...how do I append NSAttributedText to a UITextView? (in Swift 2.0+)
我正在构建一个从服务器下载项目的工具,当它们进来时,我想添加AttributedText并添加绿色(表示成功)或添加红色(表示失败).
I'm building a tool which downloads items from my server, and as they come in I want to add AttributedText with green for success or red color for fails.
为此,我相信我需要NSMutableAttributedString,但是UITextView仅具有NSattributedString,该NSattributedString无法访问appendAttributedString(attrString: NSAttributedString NSAttributedString)
To do this I believe I need NSMutableAttributedString, but the UITextView only has NSattributedString which does not have access to the appendAttributedString(attrString: NSAttributedString NSAttributedString)
因此,如果我有一个带有NSAttributedString的UITextView,上面写着红色的正在加载",我该如何附加带有绿色的成功"文本的正在加载"文本.
So if I have a UITextView with an NSAttributedString on it that says "loading" in red, how can I append the text "loading" with the text in green "success".
例如这样的
<font color="red">loading</font><font color="green">success</font>
更新
我找到了问题的答案,但我认为这不是最佳答案.
I found an answer to my question but I do not feel is an optimal answer.
let loadingMessage = NSMutableAttributedString(string: "loading...\n")
loadingMessage.addAttribute(NSStrokeColorAttributeName, value: UIColor.redColor(), range: NSRange(location: 0, length: 10))
progressWindowViewController.theTextView.attributedText = loadingMessage
loadingMessage.appendAttributedString("<font color=\"#008800\">Successfully Synced Stores...</font>\n".attributedStringFromHtml!)
progressWindowViewController.theTextView.attributedText = loadingMessage
我上面的答案是有效的,但是可以通过覆盖整个文本来实现(并且每次绘制都会继续这样做).我想知道是否存在将字符串附加到末尾以获得最佳性能的真正方法?
My answer above works but does so by overwriting the entire text (and it will continue to do so every-time it draws). I wonder if there is a true way to append the string to the end for optimal performance?
我用于HTML的扩展名
The extension I used for HTML
extension String {
var attributedStringFromHtml: NSAttributedString? {
do {
return try NSAttributedString(data: self.dataUsingEncoding(NSUTF8StringEncoding)!, options: [NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType], documentAttributes: nil)
} catch _ {
print("Cannot create attributed String")
}
return nil
}
}
推荐答案
您可以使用mutableCopy()
将NSAttributedString
转换为NSMutableAttributedString
,而copy()
会为您做相反的事情,例如:>
You can convert a NSAttributedString
to a NSMutableAttributedString
using mutableCopy()
, and copy()
will do the opposite for you, like so:
let string1 = NSAttributedString(string: "loading", attributes: [NSForegroundColorAttributeName: UIColor.redColor()])
let string2 = NSAttributedString(string: "success", attributes: [NSForegroundColorAttributeName: UIColor.greenColor()])
let newMutableString = string1.mutableCopy() as! NSMutableAttributedString
newMutableString.appendAttributedString(string2)
textView.attributedText = newMutableString.copy() as! NSAttributedString
由于mutableCopy()
和copy()
都返回AnyObject
,所以有点尴尬,因此您需要始终使用as!
将它们转换为正确的类型.
It's only a bit awkward since both mutableCopy()
and copy()
return AnyObject
, so you'll need to convert them using as!
to the correct type all the time.
这篇关于将NSAttributed文本追加到UITextview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!