UILabel with text of different color in Storyboard

Forte Zhu picture Forte Zhu · Feb 14, 2017 · Viewed 11k times · Source

I try to set UILabel with text of different colors programmatically using,

NSMutableAttributedString * string = [[NSMutableAttributedString alloc] initWithString:@"firstsecondthird"];
[string addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(0,5)];
[string addAttribute:NSForegroundColorAttributeName value:[UIColor greenColor] range:NSMakeRange(5,6)];
[string addAttribute:NSForegroundColorAttributeName value:[UIColor blueColor] range:NSMakeRange(11,5)];
[self.resultLabel setAttributedText:string];

I get what I expected. But, I am interested in doing all these stuff in storyboard,in main.storyboard. Can anyone help me in doing this.


This is what I want,

enter image description here

Answer

iPeter picture iPeter · Feb 14, 2017

To achieve that in storyboard, make the label's text attributed, then select the range and change colors accordingly.

See the image below:

enter image description here

Hope this helps.