[英]Highlighting text in the UITextView
我有一個名為Highlight
的菜單項,它調用以下功能。 該功能可以更改所選文本的顏色,並且效果很好。 但是,我不確定我是否做得正確。 我擔心如果_myTextView
( UITextView
)具有大量文本,則可能會出現性能問題。
您能否看一下,並提出一個更好的方法(如果存在)?
- (void)highlight {
NSRange selectedRange = _myTextView.selectedRange;
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc]
initWithAttributedString:_myTextView.attributedText];
[attributedString addAttribute:NSForegroundColorAttributeName
value:[UIColor redColor]
range:selectedRange];
_myTextView.attributedText = attributedString;
}
這不會引起性能問題,因為您所做的任何事情都不會太繁重。 即使有大量文本,UIKit也可以很好地處理。 這就像在設備上添加大量文本並進行測試以查看其處理方式一樣簡單。
我建議您開始將對象稱為self.myObject而不是_myObject。 這是因為如果您需要覆蓋getter,則必須返回並修復代碼以改為使用getter。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.