Hacker News new | ask | show | jobs
by nicw 4931 days ago
Adding a little more detail: To stylize your content, you create a 'mask' by specifying the character ranges that you want to change, and then applying attributes to the string

Snippet to take a gray-colored string "Hello World", and then make "World" show as a black color (from memory):

  NSString *theEntireLineOfText = @"Hello World";
  UIColor *boldColor = [UIColor blackColor];
  UIFont *myFont = [UIFont fontWithName:@"Helvetica" size:12.0];
  UIColor *baseColor = [UIColor grayColor];
  NSDictionary *subAttrs = [NSDictionary dictionaryWithObjectsAndKeys:
                              myFont, NSFontAttributeName, boldColor,NSForegroundColorAttributeName, nil];
   NSMutableAttributedString *mainText = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"%@",theEntireLineOfText] attributes:@{NSFontAttributeName:myFont,NSForegroundColorAttributeName:baseColor}];
  // Make the word "World" black"
  NSRange range = NSMakeRange(0,[@"World" length]);
        [mainText setAttributes:subAttrs range:range];