Skip navigation.
 
mlTextview is overwriting itself
FROM : Christopher Woodruff
DATE : Fri Mar 21 19:37:35 2008

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.

Regards,

Chris Woodruff

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