So I used DuckDuckGo to find "NSText syntax highlight," which led me to Uli 
Kusterer's site where there an example syntax highlighting document project.  

If I'm reading the code right, it appears Uli builds a new attributed string 
with text colors and uses that to replace the original edited range.

Could it be that even though the layout manager's temporary attributes are 
designed for purposes like syntax highlighting, folks don't actually use them 
because they don't work right during edits?  

—  

Charles


On Wednesday, December 17, 2014 at 22:03, Charles Jenkins wrote:

> 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 (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