【发布时间】:2013-06-14 04:15:20
【问题描述】:
我正在尝试使用以下代码搜索属性化 UITextView 的内容:
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当然是NSMutableAttributedString
这很好用,只是在处理大文本时速度很慢。使用包含 156,000 个字符的 UITextView 需要几秒钟才能看到更改。如果我 NSLog 循环的单个步骤,我可以看到代码执行得很快。更改需要几秒钟才能在 UITextView 中可见。
属性化的 UITextview 是否需要一段时间才能重绘?有什么办法可以加快进程吗?我尝试使用正则表达式搜索文本,但这似乎没有任何改变。
谢谢
【问题讨论】:
标签: ios6 uitextview nsattributedstring nsrange