ios6 - 在 UITextView 中使用 NSMutableAttributedString 进行 NSRange 搜索很慢

标签 ios6 uitextview nsattributedstring nsrange

我正在尝试使用以下代码搜索我的属性 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 是否只需要一段时间?有什么办法可以加快这个过程吗?我尝试使用正则表达式搜索文本,但这似乎没有任何改变。

谢谢

最佳答案

使用 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);*/

关于ios6 - 在 UITextView 中使用 NSMutableAttributedString 进行 NSRange 搜索很慢,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17160571/

相关文章:

ios - 替换什么[读者解雇ModalViewControllerAnimated :NO with?

iphone - 获取 iPhone 上 UITextView 中的光标位置?

html - 如何从 iOS UILabel 对象中的 NSAttributedString(从 HTML 转换而来)中消除多余的换行符?

ios - 重复的 UITextView 不显示原始文本中的所有文本

ios - 如何在UITextView中获取当前选择的行号?

ios - 以特定方式将 String 转换为 NSAttributedString

deprecated - ios6 中不再调用 viewDidUnload

iphone - RightBarButtonItem 大写文本?

ios6 - 升级到 Xcode4.5 iOS6 Base SDK 后运行时异常

iOS UITextView 无序/有序列表,如 Notes App