3

I have a text label inside a UITableViewCell consisting of two words.

How can I change the color of the words; making the first word green, and the second word red?

rmaddy
  • 314,917
  • 42
  • 532
  • 579
Nubaslon
  • 741
  • 2
  • 11
  • 27

4 Answers4

10
NSString *twoWords = @"Green Red";
    NSArray *components = [twoWords componentsSeparatedByString:@" "];
    NSRange greenRange = [twoWords rangeOfString:[components objectAtIndex:0]];
    NSRange redRange = [twoWords rangeOfString:[components objectAtIndex:1]];

    NSMutableAttributedString *attrString = [[NSMutableAttributedString alloc] initWithString:twoWords];

    [attrString beginEditing];
    [attrString addAttribute: NSForegroundColorAttributeName
                       value:[UIColor greenColor]
                       range:greenRange];

    [attrString addAttribute: NSForegroundColorAttributeName
                       value:[UIColor redColor]
                       range:greenRange];

    [attrString endEditing];

Then you can use attrString directly on a UILabel (> iOS 6, check Apple Documentation).

Pier-Luc Gendreau
  • 13,553
  • 4
  • 58
  • 69
cescofry
  • 3,706
  • 3
  • 26
  • 22
  • kCTStrokeColorAttributeName <-- look like the setup for enumerators... the addAttribute... takes a string value and enumerators are numbers – A'sa Dickens Jul 16 '13 at 15:29
7

The simplest way to do this would be with an in iOS 6.0 or later. You would allocate one of those and in the titleLabel (or any other object that holds text) of the UITableViewCell. If you're using the titleLabel you would do this:

[cell.titleLabel setAttributedText:yourAttributedString];

To setup the colors with an NSAttributedString, do this:

NSMutableAttributedString* attributedString = [[NSMutableAttributedString alloc] initWithString:stringToManipulate];
[attributedString beginEditing];
[attributedString addAttribute:NSForegroundColorAttributeName value:[UIColor greenColor] range:NSMakeRange(0, widthOfFisrtWord)];
[attributedString addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(widthOfFisrtWord, widthOfSecondWord)];
[attributedString endEditing];

Note that the ranges provided above using NSMakeRange won't be the ranges you need. You'll have to change the range to fit your own needs depending if the two words have a space in between them or other characters.

Apple Documentation:

A'sa Dickens
  • 2,045
  • 1
  • 16
  • 21
  • oh sweet thanks for the edit :D i was just looking at how to do that to not cluster the answer, i was able to click edit and see what you did for the formatting, much obliged – A'sa Dickens Jul 16 '13 at 15:50
  • Correct me if I'm wrong, but NSAttributedString is NOT the way to do this. The documentation you linked appears to have no way of specifying ranges for the attributes. However, your code uses NSMutableAttributedString, the documentation for which does have ranges: https://developer.apple.com/library/mac/documentation/cocoa/reference/foundation/classes/NSMutableAttributedString_Class/Reference/Reference.html#//apple_ref/occ/instm/NSMutableAttributedString/addAttribute:value:range: – prewett Dec 17 '13 at 12:49
  • that is correct, at the time i wrote the answer i guess i wasn't paying attention – A'sa Dickens Dec 17 '13 at 13:00
  • i also put [attributedString beginEditing] twice instead of [attributedString endEditing] – A'sa Dickens Dec 17 '13 at 13:02
  • No worries, just frustrated that everyone said "use NSAttributedString" and yet, the documentation appears to not have any way of doing it... But your answer pointed me in the right direction, so, +1 and thanks for that! – prewett Dec 18 '13 at 08:51
  • Actually, you can use -setAttribute, and you don't need the -beginEditing/endEditing. (At least, seems to work for me) – prewett Dec 18 '13 at 08:52
0

This question addresses getting part of a string, which you would need to do. Instead of modifying the text with BOLD though, you can use this question to get an idea of how to change the color.

Community
  • 1
  • 1
Mattiavelli
  • 888
  • 2
  • 9
  • 22
0

By using NSAttributedString string you can set two different colors.NSAttributedString

once check this one.

Community
  • 1
  • 1
Balu
  • 8,470
  • 2
  • 24
  • 41