NSTextView, Attributed string and binding colors

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

  1. varsis macrumors regular

    Joined:
    Nov 30, 2005
    #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
     
  2. kainjow Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #2
    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

    Joined:
    Nov 30, 2005
    #3
    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.
     
  4. JoshDC macrumors regular

    Joined:
    Apr 8, 2009
    #4
    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...
     
  5. varsis thread starter macrumors regular

    Joined:
    Nov 30, 2005
    #5
    Im still supporting 10.5 right now.
     
  6. Eraserhead macrumors G4

    Eraserhead

    Joined:
    Nov 3, 2005
    Location:
    UK
    #6
    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

    Joined:
    Apr 8, 2009
    #7
    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).
     
  8. varsis thread starter macrumors regular

    Joined:
    Nov 30, 2005
    #8
    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.
     
  9. Acorn macrumors 68020

    Acorn

    Joined:
    Jan 2, 2009
    Location:
    macrumors
    #9
    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

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

Share This Page