Multiple colours in an NSString
or NSMutableStrings
are not possible. So I\'ve heard a little about the NSAttributedString which was introduced wit
This solution will work for any length
NSString *strFirst = @"Anylengthtext";
NSString *strSecond = @"Anylengthtext";
NSString *strThird = @"Anylengthtext";
NSString *strComplete = [NSString stringWithFormat:@"%@ %@ %@",strFirst,strSecond,strThird];
NSMutableAttributedString *attributedString =[[NSMutableAttributedString alloc] initWithString:strComplete];
[attributedString addAttribute:NSForegroundColorAttributeName
value:[UIColor redColor]
range:[strComplete rangeOfString:strFirst]];
[attributedString addAttribute:NSForegroundColorAttributeName
value:[UIColor yellowColor]
range:[strComplete rangeOfString:strSecond]];
[attributedString addAttribute:NSForegroundColorAttributeName
value:[UIColor blueColor]
range:[strComplete rangeOfString:strThird]];
self.lblName.attributedText = attributedString;