In a UITextView, I am using attributes to highlight certain parts of the text, 
based on a user action.

Since the attributedText property is not mutable, for now I am creating a new 
string, add the attribute, and then set that for the attributed string.

It works, but I wonder if this is the correct way to do it, since I am creating 
a new string for every user action:

 // calculate the range to be highlighted
    NSRange aRange = [self calculateNewRangeToHighlight];

 // create a new NSMutableAttributedString by getting a mutable copy of the 
current attributed string
    NSMutableAttributedString *attributedString = [self.attributedText 
mutableCopy];

// remove all old highlights if present
    [attributedString removeAttribute: NSBackgroundColorAttributeName range: 
NSMakeRange(0, self.text.length)];

// add the new highlight range
    [attributedString addAttribute: NSBackgroundColorAttributeName value: 
[UIColor yellowColor] range: aRange];
   
 // now set the new attributed string to the textview
    self.attributedText = attributedString;


- Koen.


_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to