Can I set the `attributedText` property of `UILabel`

Can I set the attributedText property of a UILabel object? I tried the below code:

UILabel *label = [[UILabel alloc] init];
label.attributedText = @"asdf";

But it gives this error:

Property "attributedText" not found on object of type 'UILabel *'

#import <CoreText/CoreText.h> not working


Solution 1:

Here is a complete example of how to use an attributed text on a label:

NSString *redText = @"red text";
NSString *greenText = @"green text";
NSString *purpleBoldText = @"purple bold text";

NSString *text = [NSString stringWithFormat:@"Here are %@, %@ and %@", 
                  redText,  
                  greenText,  
                  purpleBoldText];

// If attributed text is supported (iOS6+)
if ([self.label respondsToSelector:@selector(setAttributedText:)]) {

    // Define general attributes for the entire text
    NSDictionary *attribs = @{
                              NSForegroundColorAttributeName: self.label.textColor,
                              NSFontAttributeName: self.label.font
                              };
    NSMutableAttributedString *attributedText = 
        [[NSMutableAttributedString alloc] initWithString:text
                                               attributes:attribs];

    // Red text attributes
    UIColor *redColor = [UIColor redColor];
    NSRange redTextRange = [text rangeOfString:redText];// * Notice that usage of rangeOfString in this case may cause some bugs - I use it here only for demonstration
    [attributedText setAttributes:@{NSForegroundColorAttributeName:redColor}
                            range:redTextRange];

    // Green text attributes
    UIColor *greenColor = [UIColor greenColor];
    NSRange greenTextRange = [text rangeOfString:greenText];// * Notice that usage of rangeOfString in this case may cause some bugs - I use it here only for demonstration
    [attributedText setAttributes:@{NSForegroundColorAttributeName:greenColor}
                            range:greenTextRange];

    // Purple and bold text attributes
    UIColor *purpleColor = [UIColor purpleColor];
    UIFont *boldFont = [UIFont boldSystemFontOfSize:self.label.font.pointSize];
    NSRange purpleBoldTextRange = [text rangeOfString:purpleBoldText];// * Notice that usage of rangeOfString in this case may cause some bugs - I use it here only for demonstration
    [attributedText setAttributes:@{NSForegroundColorAttributeName:purpleColor,
                                    NSFontAttributeName:boldFont}
                            range:purpleBoldTextRange];

    self.label.attributedText = attributedText;
}
// If attributed text is NOT supported (iOS5-)
else {
    self.label.text = text;
}

Solution 2:

Unfortunately, UILabel doesn't support attributed strings. You can use OHAttributedLabel instead.

Update: Since iOS6, UILabel does support attributed strings. See UILabel reference or Michael Kessler's answer below for more details.

Solution 3:

NSString *str1 = @"Hi Hello, this is plain text in red";

NSString *cardName = @"This is bold text in blue";

NSString *text = [NSString stringWithFormat:@"%@\n%@",str1,cardName];


// Define general attributes for the entire text
NSDictionary *attribs = @{
                          NSForegroundColorAttributeName:[UIColor redColor],
                          NSFontAttributeName: [UIFont fontWithName:@"Helvetica" size:12]
                          };
NSMutableAttributedString *attributedText = [[NSMutableAttributedString alloc] initWithString:text attributes:attribs];


UIFont *boldFont = [UIFont fontWithName:@"Helvetica-Bold" size:14.0];
NSRange range = [text rangeOfString:cardName];
[attributedText setAttributes:@{NSForegroundColorAttributeName: [UIColor blueColor],
                                NSFontAttributeName:boldFont} range:range];


myLabel = [[UILabel alloc] initWithFrame:CGRectZero];

myLabel.attributedText = attributedText;