sakurashinken sakurashinken - 22 days ago 20
Objective-C Question

How to check for attribute on NSMutableAttributedString

I would like to execute a block of code on a NSMutableAttributedString iff the attribute NSStrikethroughStyleAttributeName is set. Is there an easy way to check for the existence of an attribute? Excuse me before hand if the answer is in the docs.

Answer

What about this:

NSMutableAttributedString *yourString; //Initialize these things before calling the method below
NSRange range;
if ([yourString attribute:NSStrikethroughStyleAttributeName atIndex:text.length - 1 effectiveRange:&range]) {
    //doSomeStuff
}

The documentation says it returns "The value for the attribute named attributeName of the character at index, or nil if there is no such attribute.". So you just have to check if the method returns something that's its not nil.

Source: https://developer.apple.com/reference/foundation/nsattributedstring/1408174-attribute?language=objc

Comments