如何在textview中找到多个单词来改变它的颜色

时间:2016-10-22 07:15:55

标签: ios objective-c textview nsmutableattributedstring

我正在开展项目,其中我采用了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"并将其颜色更改为红色。在这里,我正在搜索单个字符串,那么我应该怎么做才能检查多个字符串,如数组

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

请帮帮我 谢谢

2 个答案:

答案 0 :(得分:1)

我实现了这样的事情

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

答案 1 :(得分:0)

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