Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Here is a complete example of how to use an attributed text on a label:</p> <pre><code>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; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload