NSTextView, Attributed string and binding colors

Discussion in 'Mac Programming' started by varsis, Jan 29, 2010.

  1. varsis macrumors regular

    Nov 30, 2005
    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...

    [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?

  2. kainjow Moderator emeritus


    Jun 15, 2000
    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?
  3. varsis thread starter macrumors regular

    Nov 30, 2005
    I have setup syntax highlighting using Regex Kit. Currently setup in the

    But Issue is I just need the colors updated not reload and look for all the regex commands.
  4. JoshDC macrumors regular

    Apr 8, 2009
    How about defining a custom attribute that defines the syntactical nature of the word (when you color them) and enumerating over that attribute using:

    - (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...
  5. varsis thread starter macrumors regular

    Nov 30, 2005
    Im still supporting 10.5 right now.
  6. Eraserhead macrumors G4


    Nov 3, 2005
    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.
  7. JoshDC macrumors regular

    Apr 8, 2009
    You could create a custom method that performs the same job as the enumeration one using:

    - (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).
  8. varsis thread starter macrumors regular

    Nov 30, 2005
    I may just have to try that. The method I found that works right now...

    	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.
  9. Acorn macrumors 68020


    Jan 2, 2009
    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.
  10. varsis thread starter macrumors regular

    Nov 30, 2005
    I could make something up, I think I will be ordering an Ipad to develop for when I have the time.

Share This Page