Search code examples
iosswift2uitextviewnsattributedstringnsmutableattributedstring

Append NSAttributed text to UITextview


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+)

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.

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)

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".

For example like this:

<font color="red">loading</font><font color="green">success</font>

Update

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?

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
    }
}

Solution

  • 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
    

    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.