In my app I need to display under line text in a label so I used following code to display underlined text
NSMutableAttributedString *attributeString = [[NSMutableAttributedString alloc] initWithString:normalString];
[attributeString addAttribute:NSUnderlineStyleAttributeName
value:[NSNumber numberWithInt:1]
range:(NSRange){0,[attributeString length]}];
wesiteAddressLabel.attributedText = attributeString;
This method and some other implementations which works fine in iOS 6.1
But when I executed in iOS 5.1 and below, app gets crashed due to the reason,
[attributeString addAttribute:NSUnderlineStyleAttributeName
value:[NSNumber numberWithInt:1]
range:(NSRange){0,[attributeString length]}];
not supported in previous versions
So I want to use respondsToSelector:
method to check if instance responds and implement another method for unsupported selector.
How I use this method?
As from the documentation:
attributedText The styled text displayed by the label.
@property(nonatomic,copy) NSAttributedString *attributedText Discussion This property is nil by default. Assigning a new value to this property also replaces the value of the text property with the same string data, albeit without any formatting information. In addition, assigning a new a value updates the values in the font, textColor, and other style-related properties so that they reflect the style information starting at location 0 in the attributed string.
Availability Available in iOS 6.0 and later. Declared In UILabel.h
You should check if the a specific UIView
element is able to respond to the attributedText
. In this case:
[wesiteAddressLabel respondsToSelector:@selector(attributedText)];
Should be enough