Использование NSAttributedString на самом деле использует другой класс :) Однако я советую вам подготовить его с помощью NSMutableAttributedString, а затем сохранить версию без изменений, поскольку ее легко читать. Во всяком случае некоторые непроверенный код должен выглядеть следующим образом:
NSMutableAttributedString* message = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"1 word 2 word"] attributes:nil];
[message addAttribute:NSForegroundColorAttributeName value:[UIColor greenColor] range:NSMakeRange(0, 1)];
[message addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(8, 1)];
Также вы можете сразу добавить NSAttributedString 's с заданными свойствами, а не устанавливать их с диапазоном:
NSMutableAttributedString* message = [[NSMutableAttributedString alloc] init];
//set text
[message appendAttributedString:[[NSAttributedString alloc] initWithString:@"1" attributes:@{
NSFontAttributeName : [UIColor greenColor]
}]];
[message appendAttributedString:[[NSAttributedString alloc] initWithString:@" word"]];
[message appendAttributedString:[[NSAttributedString alloc] initWithString:@"2" attributes:@{
NSFontAttributeName : [UIColor redColor]
}]];
[message appendAttributedString:[[NSAttributedString alloc] initWithString:@" word"]];
Что вы попробовали? –