NSTextView scroll is really slow!! How do I achieve fast scrolling like Terminal App?

  • I've read a lot threads on the email lists about how to set up an
    automatically vertically scrolling view similar to a terminal view.
    I want new text to appear at the bottom of a textview and the scroll
    bar position to remain at the bottom, unless the user has moved it to
    look at some text.  Basically I want a window that displays text
    output that behaves and performs exactly like the Terminal
    application or the Run Log in XCode.

    I have attempted to do this by appending my text data to the
    textStorage mutableString.  To control the scroll bar position, I use
    the scrollToVisibleRange method.  Unfortunately, I have read about
    and experienced the performance hit when using this method (It is
    supposedly O(N) for the number of lines).

    I know that this can be done because I see that the Run Log in Xcode
    and the Terminal have this behavior without getting the spinning ball
    when it updates more than 10 lines a second.  Can someone give me a
    hint at a more efficient way to achieve this behavior?

    Here my current implementation for the textview:

    - (void)updateReceiverConsole:(NSNotification *)aNotification
    {
    float scrollPos = [[receiverScroll verticalScroller] floatValue]; //
    Get the current scrollbar position

    NSString *aLogMessage = [aNotification object];
    if(!aLogMessage==nil)  //Append the new text to the textStorage
    mutableString
    {
      [[[receiverConsole textStorage] mutableString ]
    appendString:aLogMessage];
    }

    // Delete the old lines of text to keep the text a set number of lines
    if([[[receiverConsole textStorage] paragraphs] count]>50)
    {
      [[receiverConsole textStorage] deleteCharactersInRange:NSMakeRange
    (0, [[[[receiverConsole textStorage] paragraphs] objectAtIndex:0]
    length])];
    }

    // if the scrollbar *was all the way at the bottom before the new
    text was appended,
    // then scroll down to the bottom
    if( scrollPos == 1.0 )
    {
      NSRange range;
      range = NSMakeRange ([[receiverConsole string] length], 0);

      [receiverConsole scrollRangeToVisible: range];  //This method is O
    (N) for the number of lines!
    }
    }

    Cheers,

    Chris Woodruff

    Jet Propulsion Laboratory
    M/S: 138-206
    4800 Oak Grove Dr.
    Pasadena, CA 91109
    818-354-2412
previous month november 2007 next month
MTWTFSS
      1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30    
Go to today