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
source share