How do you clear attributes from NSMutableAttributedString?

swift 4:

I needed to remove the attributes from a textview in a reusable cell, they were transferred from one cell to another if the other was using the text property, so the text instead being only a text it was with the attributes from the previous cell. Only this worked for me, inspired by the accepted answer above:

iOS

let attr = NSMutableAttributedString(attributedString: (cell.textView?.attributedText)!)
    let originalRange = NSMakeRange(0, attr.length)
    attr.setAttributes([:], range: originalRange)
    cell.textView?.attributedText = attr
    cell.textView?.attributedText = NSMutableAttributedString(string: "", attributes: [:])
    cell.textView?.text = ""

macOS

textView.textStorage?.setAttributedString(NSAttributedString(string: ""))

You can remove all of the attributes like this:

NSMutableAttributedString *originalMutableAttributedString = //your string…

NSRange originalRange = NSMakeRange(0, originalMutableAttributedString.length);
[originalMutableAttributedString setAttributes:@{} range:originalRange];

Note that this uses setAttributes (not add). From the docs:

These new attributes replace any attributes previously associated with the characters in aRange.

If you need to do any of it conditionally, you could also enumerate the attributes and remove them one-by-one:

[originalMutableAttributedString enumerateAttributesInRange:originalRange
                                                    options:kNilOptions
                                                 usingBlock:^(NSDictionary *attrs, NSRange range, BOOL *stop) {
                                                        [attrs enumerateKeysAndObjectsUsingBlock:^(NSString *attribute, id obj, BOOL *stop) {
                                                            [originalMutableAttributedString removeAttribute:attribute range:range];
                                                        }];
                                                    }];

According to the docs this is allowed:

If this method is sent to an instance of NSMutableAttributedString, mutation (deletion, addition, or change) is allowed.


Swift 2

If string is a mutable attributed string:

string.setAttributes([:], range: NSRange(0..<string.length))

And if you want to enumerate for conditional removal:

string.enumerateAttributesInRange(NSRange(0..<string.length), options: []) { (attributes, range, _) -> Void in
    for (attribute, object) in attributes {
        string.removeAttribute(attribute, range: range)
    }
}

How I remove and add attributes of a string in a tableView cellForRowAt indexPath. Create a new NSMutableAttributedString because it is immutable

// strike through on text
    let attributeString =  NSMutableAttributedString(string: "your string")
    attributeString.addAttribute(NSAttributedString.Key.strikethroughStyle, value: 2, range: NSMakeRange(0, attributeString.length))

    let removedAttributeString = NSMutableAttributedString(string: "your string")
    removedAttributeString.removeAttribute(NSAttributedString.Key.strikethroughStyle, range: NSMakeRange(0, attributeString.length))

    if (item.done == true) {
        // applies strike through on text
        cell.textLabel?.attributedText = attributeString

    } else {
        // removes strike through on text
        cell.textLabel?.attributedText = removedAttributeString
    }