by sandofsky · 13y ago · 0 comments
If you only support iOS 6, you can use a UILabel and set the "attributedText" property.
I notice a lot of new iOS developers jump to open source libraries to abstract away important frameworks. It might save an hour, but I think it's counterproductive to not do things by hand the first time.
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];