Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

varsis

macrumors regular
Original poster
Nov 30, 2005
209
1
Okay here's the deal. I have a NSTextView. It colors the text accordingly by code... I am setting up the preferences to change the colors, but not have been successful with live updating of the text.

I have bound the NSColors to NSUserdefaults. It works, but doesn't update unless you edit the textview. I can get it to work by calling the function...

Code:
[self textStorageDidProcessEditing:[NSNotification notificationWithName:@"new" object:[myTextView textStorage]]];

But this method is far to slow... (Coloring is done through textStorageDidProcessEditing:)

Anyone have any ideas? Something im missing?

Thanks
Chris
 
It's a little unclear what you're asking. Are you wanting to figure out a faster way to update the text view? If so have you tried a simple setNeedsDisplay: ? Also what are the colors actually set to? Meaning, is it just a single color for all the text, or are you implementing some form of syntax highlighting?
 
It's a little unclear what you're asking. Are you wanting to figure out a faster way to update the text view? If so have you tried a simple setNeedsDisplay: ? Also what are the colors actually set to? Meaning, is it just a single color for all the text, or are you implementing some form of syntax highlighting?

I have setup syntax highlighting using Regex Kit. Currently setup in the
textStorageDidProcessEditing:

But Issue is I just need the colors updated not reload and look for all the regex commands.
 
How about defining a custom attribute that defines the syntactical nature of the word (when you color them) and enumerating over that attribute using:

Code:
- (void)enumerateAttribute:(NSString *)attrName inRange:(NSRange)enumerationRange options:(NSAttributedStringEnumerationOptions)opts usingBlock:(void (^)(id value, NSRange range, BOOL *stop))block

As long as 10.6 only is okay...
 
How about defining a custom attribute that defines the syntactical nature of the word (when you color them) and enumerating over that attribute using:

Code:
- (void)enumerateAttribute:(NSString *)attrName inRange:(NSRange)enumerationRange options:(NSAttributedStringEnumerationOptions)opts usingBlock:(void (^)(id value, NSRange range, BOOL *stop))block

As long as 10.6 only is okay...

Im still supporting 10.5 right now.
 
I'd be curious to know how to do this as well as I couldn't figure it out myself. And I need 10.4 support :p.
 
You could create a custom method that performs the same job as the enumeration one using:

Code:
- (id)attribute:(NSString *)attributeName atIndex:(NSUInteger)index effectiveRange:(NSRangePointer)aRange

I can't see that this should be any slower than the 10.6 method which, in my testing, seems fairly quick (finishes in <100ms on a 1500 line file highlighting over 3000 items).
 
You could create a custom method that performs the same job as the enumeration one using:

Code:
- (id)attribute:(NSString *)attributeName atIndex:(NSUInteger)index effectiveRange:(NSRangePointer)aRange

I can't see that this should be any slower than the 10.6 method which, in my testing, seems fairly quick (finishes in <100ms on a 1500 line file highlighting over 3000 items).

I may just have to try that. The method I found that works right now...

Code:
	NSTextStorage *textStorage = [textView textStorage];
	NSMutableArray *attributeRun = [NSMutableArray arrayWithArray:[textStorage attributeRuns]];
	[[attributeRun objectAtIndex:0] setForegroundColor:[NSColor blackColor]];
	[textStorage setAttributeRuns:attributeRun];

Seems to work just fine. But slow on larger files.
 
if you are making a syntex highlighter any chance you can port it to ipad. currently there is no apps in the app store that is a syntex highlighter and it would be nice to have one for my comming ipad.
 
if you are making a syntex highlighter any chance you can port it to ipad. currently there is no apps in the app store that is a syntex highlighter and it would be nice to have one for my comming ipad.

I could make something up, I think I will be ordering an Ipad to develop for when I have the time.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.