by nicw · 13y ago · 0 comments
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];No comments yet.