0

Say i have UIlabel and i want to change color of text at certain word. For example my label text is "Shop now and get up 50% off Select shoes Limited time offer"

i which "50%" and "Limited" want change color

predefined some word if accrue in label text then change color. fist find and then change color and font or sub string.

Umer Afzal
  • 371
  • 1
  • 2
  • 18

2 Answers2

1

To do this use NSAttributedString:

 NSMutableAttributedString *text = 
 [[NSMutableAttributedString alloc] 
   initWithAttributedString: label.attributedText];

[text addAttribute:NSForegroundColorAttributeName 
             value:[UIColor redColor] 
             range:NSMakeRange(10, 1)];
[label setAttributedText: text];

For details :https://github.com/joaoffcosta/UILabel-FormattedText

Logic
  • 705
  • 1
  • 9
  • 27
0

You can use NSMutableAttributedString

    NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:@"Shop now and get up 50% off Select shoes Limited time offer"];
    [str addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(, )];
    [str addAttribute:NSForegroundColorAttributeName value:[UIColor whiteColor] range:NSMakeRange(, )];
Sreejith
  • 1,345
  • 13
  • 25
  • dear how to find predefined words that want to change color like "50%" and "Limited" i not now range run time check if 50% and Limited in this label then change color. – Umer Afzal Oct 23 '14 at 05:34