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