使用 NSMutableAttributedString 更改 UITextView 中小字 ('in') 的颜色

Change Colour of Small Words ('in') in UITextView using NSMutableAttributedString

我正在使用 NSMutableAttributedString 在用户键入时更改 UITextView 中的文本。当用户键入“#HELLO#”或“#TEST#”或“#test#”时,这些字符串应为红色(仅作为示例)。

- (void)textViewDidChange:(UITextView *)textView
{    
    NSString *textViewText = textView.text;
    NSMutableAttributedString * string = [[NSMutableAttributedString alloc]initWithString:textViewText];

    NSString *space = @" ";
    NSArray *words =[textView.text componentsSeparatedByString:space];

    for (NSString *word in words) {

        if ([word isEqualToString:@"#HELLO#"] || [word isEqualToString:@"#TEST#"] || [word isEqualToString:@"#test#"]) {
            NSRange range=[textView.text rangeOfString:word];
            [string addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:range];
        }
        else{

            NSRange range=[textView.text rangeOfString:word];
                [string addAttribute:NSForegroundColorAttributeName value:[UIColor whiteColor] range:range];

        }
    }

    [string addAttribute:NSFontAttributeName
                   value:[UIFont fontWithName:@"HelveticaNeue-Light" size:20.0]
                   range:NSMakeRange(0, [textView.text length])];

    [textView setAttributedText:string];
}

这几乎适用于每个单词,'in' 除外。当我输入时,'in' 是黑色而不是白色 ([UIColor whiteColor])。如果我键入 't',“#test#”中的 't' 变为白色。

我真的很困惑,有人可以帮我吗?我认为 else 部分应该捕获这些字符串。谢谢

我试过你的代码。我想问题是你设置的范围。因为它总是一个词,所以它设置了该特定词第一次出现的颜色属性。无论是#HELLO# 还是 .尝试重复输入由 space 分隔的特定字符串,您将始终获得相同的输出。我对您的代码做了一些更改,您可以在下面看到。试试吧。

  - (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];
}