如何在文本视图中查找多个单词以更改其颜色

How to find mutiple words in textview for changing color of it

我正在做一个项目,我在下面使用了一个 texview 及其委托方法

-(void)textViewDidChange:(UITextView *)textView
{
    NSMutableAttributedString *attstr = [[NSMutableAttributedString alloc]initWithString:textView.text];
    NSUInteger characterCount = [attstr length];
    NSString *str = @"football";
    NSUInteger searchlength = [str length];
    NSRange range1 = NSMakeRange(0, attstr.length);
    while (range1.location != NSNotFound) {
        range1 =[attstr.string rangeOfString:str options:0 range:range1];
        if (range1.location !=NSNotFound) {
            [attstr addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(range1.location, searchlength)];
            [attstr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:20.0]  range:range1];
            range1 = NSMakeRange(range1.location + range1.length, characterCount -(range1.location + range1.length));
            textView.attributedText = attstr;
        }
    }
}

现在,在该方法中,我找到关键字 "football" 并将其颜色更改为红色。我在这里搜索单个字符串,那么我应该如何检查多个字符串,如 array

我的问题是如何在 textview 的文本中一次找到多个字符串。

请帮助我
谢谢

NSArray *arr = @[@"football",@"player",@"match"];

- (void)textViewDidChange:(UITextView *)textView
{
    NSString *text = textView.text;

    NSMutableAttributedString *mutableAttributedString = [[NSMutableAttributedString alloc] initWithString:text];        

    NSRange range = NSMakeRange(0 ,text.length);
    NSRegularExpression *regex;
    for (int i=0; i<arr.count; i++) {
        NSString *obj = [arr objectAtIndex:i];
        regex = [NSRegularExpression regularExpressionWithPattern:obj options:kNilOptions error:nil];// Case sensitive 

        [regex enumerateMatchesInString:text options:kNilOptions range:range usingBlock:^(NSTextCheckingResult *result, NSMatchingFlags flags, BOOL *stop) {

            NSRange subStringRange = [result rangeAtIndex:0];
            [mutableAttributedString addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:subStringRange];
        }];

        txt_out.attributedText = mutableAttributedString;
    }   
}

我是这样实现的

-(void)textViewDidChange:(UITextView *)textView
{
    NSMutableAttributedString *attstr = [[NSMutableAttributedString alloc]initWithString:textView.text];
    NSUInteger characterCount = [attstr length];
    NSArray *arr = [[NSArray alloc]initWithObjects:@"football",@"player",nil];

    for (int i=0; i<arr.count; i++) {

    NSUInteger searchlength = [[NSString stringWithFormat:@"%@",[arr objectAtIndex:i]] length];
    NSRange range1 = NSMakeRange(0, attstr.length);

    while (range1.location != NSNotFound) {
        range1 =[attstr.string rangeOfString:[NSString stringWithFormat:@"%@",[arr objectAtIndex:i]] options:0 range:range1];
        if (range1.location !=NSNotFound) {
            [attstr addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(range1.location, searchlength)];
            [attstr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:20.0]  range:range1];
            range1 = NSMakeRange(range1.location + range1.length, characterCount -(range1.location + range1.length));
            textView.attributedText = attstr;
        }
    }
}