I’m creating a text editor with a NSTextView inside a NSScrollView and would like to automatically scroll the textview whenever the user inserts some text, so that the inserted text is vertically centered on the window (or on the textview). I found this sample code to scroll to the top of a scrollview, but can’t really understand how it works. I’ve already messed around with this code, but my textview just seems to jump up and down without any logics. Can anyone help me?
- (void)scrollToTop:sender;
{
NSPoint newScrollOrigin;
// assume that the scrollview is an existing variable
if ([[scrollview documentView] isFlipped]) {
newScrollOrigin=NSMakePoint(0.0,0.0);
} else {
newScrollOrigin=NSMakePoint(0.0,NSMaxY([[scrollview documentView] frame])
-NSHeight([[scrollview contentView] bounds]));
}
[[scrollview documentView] scrollPoint:newScrollOrigin];
}
Well, I found a solution:
where textView is the
IBOutletto the textview andscrollingis the distance in pixels from the bottom where the insertion point should be set. Just play around with it to understand how it works.