Finding NSRange in a UITextView with NSMutableAttributedString slow

I am trying to search the contents of my UITextView attribute 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 , of course, a NSMutableAttributedString

This works great, except that it is very slow with large texts. With a UITextView containing 156,000 characters, changes are required to become visible. If I am an NSLog for the individual steps of the loop, I can see that the code runs quickly. It takes a few seconds for the changes to become visible in the UITextView.

Does it take some time for the reassigned UITextview to redraw? Something to speed up the process? I tried to find text using regular expressions, but that didn't change anything.

thanks

+4
source share
1 answer

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);*/ 
0
source

Source: https://habr.com/ru/post/1486740/


All Articles