How to clear attributes from NSMutableAttributedString?

How do you set all attributes in NSMutableAttributedString nothing? Do you need to list them and delete them?

I do not want to create a new one. I am working on textStorage in an NSTextView . Setting a new line resets the cursor position in the NSTextView and starts the delegate.

+5
source share
4 answers

You can remove all attributes like this:

 NSMutableAttributedString *originalMutableAttributedString = //your string… NSRange originalRange = NSMakeRange(0, originalMutableAttributedString.length); [originalMutableAttributedString setAttributes:@{} range:originalRange]; 

Please note that set attributes are used for this (do not add ). From the docs:

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

If you need to do any of this conditionally, you can also list the attributes and delete them one at a time:

 [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]; }]; }]; 

In accordance with the documents, this is allowed:

If this method is sent to an NSMutableAttributedString instance, mutation is allowed (delete, add, or modify).


Swift 2

If string is a mutable string:

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

And if you want to list for conditional deletion:

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

NSAttributedString is immutable, so you cannot do much with its instance. One option is to use the string property for NSAttributedString , create a new NSMutableAttributedString with it and apply the desired attributes, or make a mutableCopy your NSAttributedString instance and change the current attributes for the ranges.

+1
source

First of all, it should be NSMutableAttributedString, because it has a removeAttribute (name: String, range: NSRange) method, and yes, it looks like you have to list them and remove them.

Why not try this approach (Swift):

 let s1 = <some NSAttributedString> var s2 = NSMutableAttriutedString(string: s1.string()) 
0
source

quick 4:

I needed to remove the attributes from the text view in the reusable cell, they were transferred from one cell to another if the other uses the text property, so the text instead is just the text that 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: "")) 
0
source

Source: https://habr.com/ru/post/1214745/


All Articles