Ios 使用NSMutableAttributedString更改UITextView中的小词(';)的颜色
我正在使用NSMutableAttributeString在用户键入时更改UITextView中的文本。当用户键入“#HELLO#”或“#TEST#”或“#TEST#”时,这些字符串应为红色(仅为示例) 这几乎适用于所有单词,除了“in”。当我输入时,“in”是黑色而不是白色(Ios 使用NSMutableAttributedString更改UITextView中的小词(';)的颜色,ios,objective-c,cocoa-touch,uitextview,nsattributedstring,Ios,Objective C,Cocoa Touch,Uitextview,Nsattributedstring,我正在使用NSMutableAttributeString在用户键入时更改UITextView中的文本。当用户键入“#HELLO#”或“#TEST#”或“#TEST#”时,这些字符串应为红色(仅为示例) 这几乎适用于所有单词,除了“in”。当我输入时,“in”是黑色而不是白色([UIColor-whiteColor])。如果我键入“t”,则“#test”中的“t”将变为白色 我真的很困惑,有人能帮我吗?我认为 EX/部分应该抓住这些字符串。谢谢。 < p>我尝试了你的代码。我猜问题是你设置的范围
[UIColor-whiteColor]
)。如果我键入“t”,则“#test”中的“t”将变为白色
<>我真的很困惑,有人能帮我吗?我认为
- (void)textViewDidChange:(UITextView *)textView
{
NSString *textViewText = textView.text;
NSLog(@"Text view Text %@" , textViewText );
NSMutableAttributedString * string = [[NSMutableAttributedString alloc]initWithString:textViewText];
NSString *space = @" ";
NSArray *words =[textView.text componentsSeparatedByString:space];
for(NSString *word in words){
NSLog(@"WORD %@" , word);
if ([word isEqualToString:@"#HELLO#"] || [word isEqualToString:@"#TEST#"] || [word isEqualToString:@"#test#"]) {
NSRange range = NSMakeRange(0, string.length);
while(range.location != NSNotFound)
{
range = [[string string] rangeOfString:word options:0 range:range];
if(range.location != NSNotFound)
{
[string addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(range.location, word.length)];
range = NSMakeRange(range.location + range.length, string.length - (range.location + range.length));
}
}
}
else{
NSRange range = NSMakeRange(0,string.length);
while(range.location != NSNotFound)
{
range = [[string string] rangeOfString:word options:0 range:range];
if(range.location != NSNotFound)
{
[string addAttribute:NSForegroundColorAttributeName value:[UIColor whiteColor] range:range];
range = NSMakeRange(range.location + range.length, string.length - (range.location + range.length));
}
}
}
}
[string addAttribute:NSFontAttributeName
value:[UIFont fontWithName:@"HelveticaNeue-Light" size:20.0]
range:NSMakeRange(0, [textView.text length])];
[textView setAttributedText:string];
}
下面可能会出现一些问题:
rangeOfString:
只返回第一次出现的情况。因此,如果您有\HELLO\HELLO\35;
,则只有第一个将被着色。或者,例如,\HELLO\35;输出(只有第一个输出将为白色)加上,在开始时,使用
NSString*textViewText=textView.AttributeText;`以某种方式不记得默认情况下,如果您有一个NSAttributedString
,它将把string
属性保留在它的文本中。
- (void)textViewDidChange:(UITextView *)textView
{
NSString *textViewText = textView.text;
NSLog(@"Text view Text %@" , textViewText );
NSMutableAttributedString * string = [[NSMutableAttributedString alloc]initWithString:textViewText];
NSString *space = @" ";
NSArray *words =[textView.text componentsSeparatedByString:space];
for(NSString *word in words){
NSLog(@"WORD %@" , word);
if ([word isEqualToString:@"#HELLO#"] || [word isEqualToString:@"#TEST#"] || [word isEqualToString:@"#test#"]) {
NSRange range = NSMakeRange(0, string.length);
while(range.location != NSNotFound)
{
range = [[string string] rangeOfString:word options:0 range:range];
if(range.location != NSNotFound)
{
[string addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(range.location, word.length)];
range = NSMakeRange(range.location + range.length, string.length - (range.location + range.length));
}
}
}
else{
NSRange range = NSMakeRange(0,string.length);
while(range.location != NSNotFound)
{
range = [[string string] rangeOfString:word options:0 range:range];
if(range.location != NSNotFound)
{
[string addAttribute:NSForegroundColorAttributeName value:[UIColor whiteColor] range:range];
range = NSMakeRange(range.location + range.length, string.length - (range.location + range.length));
}
}
}
}
[string addAttribute:NSFontAttributeName
value:[UIFont fontWithName:@"HelveticaNeue-Light" size:20.0]
range:NSMakeRange(0, [textView.text length])];
[textView setAttributedText:string];
}