简体   繁体   中英

Highlighting text in the UITextView

I have a menu item called Highlight which calls the below function. The function changes the colour of the selected text and works fine. However, I'm not sure I'm doing it correctly. I am worried that performance problems may occur if the _myTextView (an UITextView ) has large amount of text.

Could you have a look at it and suggest a better way if exist?

- (void)highlight {

    NSRange selectedRange = _myTextView.selectedRange;

    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc]        
                              initWithAttributedString:_myTextView.attributedText];

    [attributedString addAttribute:NSForegroundColorAttributeName 
                      value:[UIColor redColor] 
                      range:selectedRange];

    _myTextView.attributedText = attributedString;

}

This shouldn't cause performance issues since you aren't doing anything too heavy. Even with a large amount of text, UIKit will handle this well. This is as easy as just adding a large amount of text and testing on the device to see how it handles it.

I will suggest you start referring to your objects as self.myObject instead of _myObject. This is because if you ever need to override the getter, you'll have to go back and fix your code to use the getter instead.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM