Is it possible to have one NSStringthat has the first word one font size / color and the next word a different font size and color?
I thought maybe NSAttributedString- but not sure if this is the way to go?
I tried putting two NSStringin NSAttributedString, but that didn't work.
We are looking for a UILabel that has something like this:
LARGEString (smallString)
Working example
So far I have come up with the following:
UIFont *objectNameFont = [UIFont systemFontOfSize:14.f];
UIFont *itemsFont = [UIFont systemFontOfSize:12.f];
NSDictionary *objectNameDict = [NSDictionary dictionaryWithObject:objectNameFont forKey:NSFontAttributeName];
NSDictionary *objectItemDict = [NSDictionary dictionaryWithObject:itemsFont forKey:NSFontAttributeName];
NSString *labelFullName = [NSString stringWithFormat:@"%@ (%@)", object.name, object.items];
NSArray *components = [labelFullName componentsSeparatedByString:@" ("];
NSRange objectNameRange = [labelFullName rangeOfString:[components objectAtIndex:0]];
NSRange objectItemRange = [labelFullName rangeOfString:[components objectAtIndex:1]];
NSMutableAttributedString *attrString = [[NSMutableAttributedString alloc] initWithString:labelFullName];
[attrString beginEditing];
[attrString addAttribute: NSForegroundColorAttributeName
value:[UIColor colorWithRed:0.667 green:0.667 blue:0.667 alpha:1]
range:objectItemRange];
[attrString addAttribute: NSForegroundColorAttributeName
value:[UIColor colorWithRed:0.271 green:0.271 blue:0.271 alpha:1]
range:objectNameRange];
[attrString addAttributes:objectNameDict range:objectNameRange];
[attrString addAttributes:objectItemDict range:objectItemRange];
[attrString endEditing];
cell.title.attributedText = attrString;
return cell;
It seems that I work as I need. However, the two-line separator "(" is black, and I need it to be the same color as the color object.itemsspecified here:
[attrString addAttribute: NSForegroundColorAttributeName value:[UIColor colorWithRed:0.667 green:0.667 blue:0.667 alpha:1] range:objectItemRange];
SOLUTION FOUND
I found a solution that works for me:
NSArray objectAtIndex, NSRange
?
.