Skip navigation.
 
mlRe: Textview is overwriting itself
FROM : Ross Carter
DATE : Mon Mar 24 17:21:57 2008

On Mar 21, 2008, at 2:37 PM, Christopher Woodruff wrote:
> I used a textview to work as an automatically scrolling log window 
> for a real time data output feed.  I limit the size of the 
> textstorage programmatically.  When I fill the textstorage to my 
> programmatic size limit (currently is 16384 characters), the 
> textview overwrites itself momentarily.  By overwrite, I mean that 
> the new text displays on top of the existing text, such that you 
> can't read it anymore.  The rate I update the textview is about 1-3 
> times a second.
>
> I have posted a few screenshots of this behavior here: http://picasaweb.google.com/cswoodruff/CocoaTroubleshooting/photo#5180264259131797666
> .
>
> Here are the relevent code chunks:
>
> - (void)handleNewData:(id)newData
> {
>     scrollPos [[receiverScroll verticalScroller] floatValue];        //Get 
> the current scrollbar position
>     
>     if(newData ! nil)
>     {
>         NSRange endRange;
>         endRange.location [[receiverConsole textStorage] length];
>         endRange.length 0;
>         [receiverConsole replaceCharactersInRange:endRange 
> withString:newData];        //Append the new text to the textStorage 
> mutableString
>     }
>     
>     if([[receiverConsole textStorage] length] > TEXT_SIZE)
>     {
>         [self deleteToSize];
>     }
> }
>
> - (void)deleteToSize
> {
>     int charToDelete [[receiverConsole textStorage] length] - TEXT_SIZE;
>     [[receiverConsole textStorage] 
> deleteCharactersInRange:NSMakeRange(0, charToDelete)];
> }
>
> - (void)scrollToBottom
> {
>     //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 endRange;
>         endRange.location [[receiverConsole textStorage] length];
>         endRange.length 0;
>         [receiverConsole scrollRangeToVisible:endRange];
>     }
> }
>
>
> In the Init method:
>     [receiverScroll setPostsFrameChangedNotifications:YES];
>     [[NSNotificationCenter defaultCenter] addObserver:self 
> selector:@selector(scrollToBottom) 
> name:NSViewFrameDidChangeNotification object:nil];
>
> I am running this code on OSX 10.4.11 on a MacBook Pro 2.4 GHz.  If 
> anyone has any idea of what I'm doing wrong here, please let me 
> know.  I still consider myself a beginner at Objective-C / Cocoa, so 
> I welcome ideas on how to make this better.
>


Chris, since no one else has chimed in, I'll make a suggestion. Rather 
than use NSViewFrameDidChangeNotification and scrollRangeToVisible, 
try NSViewBoundsDidChangeNotification and scrollToPoint:. The 
technique is described in the Scroll View Programming Guide under 
"Synchronizing Scroll Views," http://developer.apple.com/documentation/Cocoa/Conceptual/NSScrollViewGuide/Articles/SynchroScroll.html#/
/apple_ref/doc/uid/TP40003537

If that doesn't work then I'd give the layout manager a chance to 
finish layout before adjusting the scroll view. The NSLayoutManager 
delegate method didCompleteLayoutForTextContainer:atEnd: will tell you 
when layout is complete.

Ross

Related mailsAuthorDate
mlTextview is overwriting itself Christopher Woodru… Mar 21, 19:37
mlRe: Textview is overwriting itself Ross Carter Mar 24, 17:21