• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
updating attributes in a UITextView
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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

  • Prev by Date: Re: Convert spelled out numbers to digits?
  • Next by Date: Working Programmatically with Bound Controller's Selection
  • Previous by thread: Re: Convert spelled out numbers to digits?
  • Next by thread: Working Programmatically with Bound Controller's Selection
  • Index(es):
    • Date
    • Thread