1

I have a label Like,

& text should be like this

I want to display this single label with different font colors.

i.e..,

"First" string should be in redcolor.

"&" text should be roundcornerd and lightgraycolor.

Vinodh
  • 5,262
  • 4
  • 38
  • 68

3 Answers3

0

Use nsattributedstring https://developer.apple.com/reference/foundation/nsattributedstring

Example use How do you use NSAttributedString?

Community
  • 1
  • 1
0

try this

     NSMutableAttributedString * string = [[NSMutableAttributedString alloc] initWithString:singlestoreNode.objSeName];
                [string addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(0,2)];
                [string addAttribute:NSForegroundColorAttributeName value:[UIColor greenColor] range:NSMakeRange(2,3)];

               yourlable.attributedText = string;

yourlable.layer.cornerRadius = 8.0;
AFTAB MUHAMMED KHAN
  • 2,189
  • 3
  • 18
  • 24
0

You can achieve that by creating 3 labels.

Set the colors for the label like this:

NSMutableAttributedString * string = [[NSMutableAttributedString alloc] initWithString:lblTemp.text];
[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)];
lblTemp.attributedText = string;

For the round background in "&" you can do the following:

[yourUILabel sizeToFit];
yourUILabel.backgroundColor = [UIColor lightGrayColor];
yourUILabel.layer.masksToBounds = YES;
yourUILabel.layer.cornerRadius = yourUILabel.frame.size.width/2;

Then you just need to align everything.

Filipe Sá
  • 372
  • 1
  • 5
  • 15