用于为我的字符串NSMutableAttributedString
着色UITextField
,但随后用户无法剪切、复制或删除字符串。例如,使用下面的代码,如果我输入“blue red @green”,然后尝试删除蓝色或剪掉蓝色,当我尝试光标移动到UITextfield
?中的最后一个字母时
有什么建议么?
- (void)colorText {
NSMutableAttributedString * string = [[NSMutableAttributedString alloc]initWithString:self.thing.text];
NSArray *words=[self.thing.text componentsSeparatedByString:@" "];
for (NSString *word in words) {
if([word isEqualToString:@""]) {continue;};
if ([word hasPrefix:@"@"]) {
NSRange range=[self.thing.text rangeOfString:word];
[string addAttribute:NSForegroundColorAttributeName value:[UIColor greenColor] range:range];
} else {
NSRange range=[self.thing.text rangeOfString:word];
[string addAttribute:NSForegroundColorAttributeName value:[UIColor darkGrayColor] range:range];
}
}
[self.thing setAttributedText:string];
}