views:

452

answers:

2

Multiple colours in an NSString or NSMutableStrings are not possible. So I've heard a little about the NSAttributedString which was introduced with the iPad SDK 3.2 (or around 3.2) and is available on the iPhone as of iPhone SDK 4.0 beta.

I would like to have a string that has three colours.

The reason I don't use 3 separate NSStrings, is because the length of each of the three NSAttributedString substrings will be changing often and so I would prefer not to use any calculations to re-position 3 separate NSString objects.

If it's possible using NSAttributedString how do I make the following - (if not possible with NSAttributed string how would you do it):

alt text

Edit: Remember, @"first", @"second" and @"third" will be replaced by other strings at any time. So using hardcoded NSRange values won't work.

+2  A: 

When building attributed strings, I prefer to use the mutable subclass, just to keep things cleaner.

That being said, here's how you create a tri-color attributed string:

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)];

typed in a browser. caveat implementor

Obviously you're not going to hard-code in the ranges like this. Perhaps instead you could do something like:

NSDictionary * wordToColorMapping = ....;  //an NSDictionary of NSString => UIColor pairs
NSMutableAttributedString * string = [[NSMutableAttributedString alloc] initWithString:@""];
for (NSString * word in wordToColorMapping) {
  UIColor * color = [wordToColorMapping objectForKey:word];
  NSDictionary * attributes = [NSDictionary dictionaryWithObject:color forKey:NSForegroundColorAttributeName];
  NSAttributedString * subString = [[NSAttributedString alloc] initWithString:word attributes:attributes];
  [string appendAttributedString:subString];
  [subString release];
}

//display string
Dave DeLong
Thanks Dave. This should work, looks like I just need to replace the hardcoded `NSMakeRange` values with `NSMakeRange(0,[string length])` etc,etc for each string. +1
Brock Woolf
I am very interested in how to display this string? thanks
Jack
@Jack search the docs for "setAttributed". That will let you know about most controls that accept attributed strings
Dave DeLong
+1  A: 

I'd been wanting to do much the same thing, but compiling for the iPad I can't find the attribute constants (e.g., NSForegroundColorAttributeName). I've imported Foundation/NSAttributedString.h, but that does not define it. I hope I'm missing something really stupid...?

Then, here's the next question. I want to just put the attributed string into a label. Am I just off track in the first place?

Feldur