Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSTextView scrolling line by line

When moving from line to line in a textview with "up" and "down" keys, it exhibits this scrolling behavior - when the cursor is at the very top and I hit "up" (and vice versa), it scrolls the document half page up, that is, the current line is now in the middle of the textview.

Is it possible to disable this behavior? Is it possible to make it scroll by just one line? So that the current line stays always at the top (or bottom)?

like image 787
Ecir Hana Avatar asked Jul 31 '12 11:07

Ecir Hana


2 Answers

I know I come a little late to the party, but since this hasn't been properly answered, I'll give it a try. I had the same problem with NSTextViews. When using -scrollRangeToVisible to bring the insert position to the visible rect of the view, it had this habit of scrolling as to put the caret in the (vertical)middle of the screen. What I did was use [NSView scrollRectToVisible] instead, as it scrolls the minimum distance needed to bring a rect to the visible rect of the view:

NSRange caretRng = NSMakeRange(caretLocation, 0);
NSLayoutManager* lm = [view layoutManager];
NSRange glyphRange = [lm glyphRangeForCharacterRange:caretRng actualCharacterRange:nil];
NSRect glyphRect = [lm boundingRectForGlyphRange:glyphRange inTextContainer:[view textContainer]];
[view scrollRectToVisible:glyphRect];

Hope it helps!

like image 190
Ibazan Avatar answered Sep 25 '22 01:09

Ibazan


NSTextView (via NSText) has a method called -scrollRangeToVisible:. Consider subclassing NSTextView and overriding -scrollRangeToVisible to specify your own scrolling behavior directly.

If you need more specific help with getting the behavior right, post a separate question specifying exactly the behavior you want and the code you tried to get it.

like image 28
Joshua Nozzi Avatar answered Sep 22 '22 01:09

Joshua Nozzi