sakurashinken sakurashinken - 6 months ago 81
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.


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]) {

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.