4

I am trying to search through the contents of my attributed UITextView with the following code:

NSRange range = NSMakeRange(0, haystack.length);

range = [haystack rangeOfString:searchText options:NSCaseInsensitiveSearch range:range];

while (range.location != NSNotFound)
{
    [_attrString addAttribute:NSBackgroundColorAttributeName value:[UIColor yellowColor] range:NSMakeRange(range.location, range.length)];

    range = NSMakeRange(range.location+range.length, haystack.length-(range.location+range.length));
    range = [haystack rangeOfString:searchText options:NSCaseInsensitiveSearch range:range locale:nil];
}

...

_textView.attributedText = _attrString;

_attrString is of course a NSMutableAttributedString

This works fine except it is very slow with large texts. With a UITextView containing 156,000 characters it takes a couple of seconds for the changes to become visible. If I NSLog the single steps of the loop I can see that the code executes fast. It takes a couple of seconds for the changes to become visible in the UITextView.

Does it just take a while for the attributed UITextview to redraw? Is do anything to speed up the process? I tried searching through the text with regular expressions, but that didn't seem to change anything.

Thanks

Joseph
  • 9,171
  • 8
  • 41
  • 67

1 Answers1

0

Use NSPredicate

UITextField *txtFld=(UITextField *)sender;
    //    if (![txtFld.text isEqualToString:@""]) {
    //[self getSearchResult:txtFld.text];
    //    }else{
    //        [self makeViewSmall];
    //    }

    // For string kind of values:

    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF contains[cd] %@", txtFld.text];
    NSArray *results = [[sortedDic valueForKey:@"locations"] filteredArrayUsingPredicate:predicate];
    NSLog(@"Result=%@",results);*/
Vizllx
  • 9,135
  • 1
  • 41
  • 79