我必须更改UITextView的文本,但保持属性的映射不变。但是当我更改文本时,属性映射也发生了变化。例如
- (void)viewDidLoad {
[super viewDidLoad];
NSMutableAttributedString *attString=[[NSMutableAttributedString alloc] initWithString:@"how are you"];
[attString addAttribute:NSForegroundColorAttributeName
value:[UIColor redColor]
range:NSMakeRange(0, 1)];
[attString addAttribute:NSForegroundColorAttributeName
value:[UIColor greenColor]
range:NSMakeRange(1, 1)];
[attString addAttribute:NSForegroundColorAttributeName
value:[UIColor blueColor]
range:NSMakeRange(2, 1)];
self.textView.attributedText=attString;
}
- (IBAction)changeText
{
NSMutableAttributedString *attString=[self.textView.attributedText mutableCopy];
[attString.mutableString setString:@"Nice to meet you"];
self.textView.attributedText=attString;
}
当触发changeText时,我想要" N"是红色的,"我"是绿色的," c"是蓝色的。但真正的结果是整个字符串是红色的。如何更改文本,同时保持属性的映射不变?