• 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
NSTextView and Syntax Highlighting
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

NSTextView and Syntax Highlighting


  • Subject: NSTextView and Syntax Highlighting
  • From: Charles Jenkins <email@hidden>
  • Date: Wed, 17 Dec 2014 22:03:32 -0500

In my text-editing app, there are some special characters I’d like to highlight whenever they are entered. I developed a function for this purpose, found in my subclass of NSTextView:

-(void)setTemporaryAttributes:(NSDictionary*)attributes
         forSpecialCharacters:(NSCharacterSet*)set
                      inRange:(NSRange)searchRange
{
  NSLayoutManager* lm = self.layoutManager;
  NSTextStorage* ts = self.textStorage;
  NSString* str = ts.string;
  NSUInteger beyondIx = searchRange.location + searchRange.length;

  NSLog( @"Searching for special chars in: %@", [str substringWithRange:searchRange]);

  while ( true ) {
    NSRange foundRange = [str rangeOfCharacterFromSet:set
                                              options:0
                                                range:searchRange];
    if ( foundRange.location == NSNotFound ) {
      break;
    }
    [lm setTemporaryAttributes:attributes forCharacterRange:foundRange];
    NSUInteger startIx = foundRange.location + foundRange.length;
    if ( startIx >= beyondIx ) {
      break;
    }
    searchRange = NSMakeRange( startIx, beyondIx - startIx );
  }

}


If I call this right after loading text into the Text View, it works perfectly and all the highlights appear in the right places.

But if I call it in response to NSTextStorageDelegate’s textStorageDidProcessEditing:, it still finds the right characters in the right ranges, but the highlighting appears in the wrong places, as if the Layout Manager is not in sync and the ranges sent to setTemporaryAttributes:forCharacterRange: don’t mean the same thing.

Is there a different place other than textStorageDidProcessEditing: that I should do this, or is there something I should be doing to sync up the Layout Manager before setting temporary attributes?

—

Charles Jenkins

_______________________________________________

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


  • Follow-Ups:
    • Re: NSTextView and Syntax Highlighting
      • From: Charles Jenkins <email@hidden>
  • Prev by Date: UIImagePickerController confused by landscape-only layout
  • Next by Date: Yosemite-like Toolbar: Segmented Controls, Buttons, Template Images?
  • Previous by thread: UIImagePickerController confused by landscape-only layout
  • Next by thread: Re: NSTextView and Syntax Highlighting
  • Index(es):
    • Date
    • Thread