Tag: 取代了

更改NSAttributedString中子string的属性

这个问题可能是这个问题的重复。 但是答案不适合我,我想要更具体。 我有一个NSString ,但我需要一个NS(Mutable)AttributedString和这个string中的一些单词应给予不同的颜色。 我试过这个: NSString *text = @"This is the text and i want to replace something"; NSDictionary *attributes = @ {NSForegroundColorAttributeName : [UIColor redColor]}; NSMutableAttributedString *subString = [[NSMutableAttributedString alloc] initWithString:@"AND" attributes:attributes]; NSMutableAttributedString *newText = [[NSMutableAttributedString alloc] initWithString:text]; newText = [[newText mutableString] stringByReplacingOccurrencesOfString:@"and" withString:[subString mutableString]]; “和”应该是大写的红色。 该文件说,mutableString保持属性映射。 但是用我的替代品,我在赋值的右侧(在我的代码片段的最后一行)没有更多的attributesString。 我怎样才能得到我想要的? ;)