1

In my application I am displaying a text in my label like this

cell.lblUserDetails.text=[NSString stringWithFormat:@"%@ and %@ other likes your post", [dictionary valueForKey:@"username"], [dictionary valueForKey:@"CuriousCount"]];

Here I want to highlight Username and CuriousCount (in red color) and also username and CuriousCount should be clickable.

Qantas 94 Heavy
  • 15,750
  • 31
  • 68
  • 83
Subramanian Raj
  • 389
  • 2
  • 5
  • 16
  • try to use NSAttributedString..use this link http://stackoverflow.com/questions/14287386/change-string-color-with-nsattributedstring – Dharma May 08 '15 at 11:08

1 Answers1

3
UIColor *color = [UIColor redColor];
NSDictionary *attrs = @{ NSForegroundColorAttributeName : color };
NSAttributedString *nameStr = [[NSAttributedString alloc] initWithString:[dictionary valueForKey:@"username"] attributes:attrs];
NSAttributedString *countStr = [[NSAttributedString alloc] initWithString:[dictionary valueForKey:@"CuriousCount"] attributes:attrs];

NSMutableAttributedString *string = [[NSMutableAttributedString alloc] init];
[string appendAttributedString:nameStr];
[string appendAttributedString:[[NSAttributedString alloc] initWithString:@" and "]];
[string appendAttributedString:countStr];
[string appendAttributedString:[[NSAttributedString alloc] initWithString:@" other likes your post"]];

cell.lblUserDetails.attributedText = string;
Aneeq Anwar
  • 1,282
  • 7
  • 20