updating attributes in a UITextView
updating attributes in a UITextView
- Subject: updating attributes in a UITextView
- From: Koen van der Drift <email@hidden>
- Date: Wed, 27 Mar 2013 11:49:44 -0400
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 (email@hidden)
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:
This email sent to email@hidden