NSTextView row-by-row scrolling

When moving from line to line in text form with the up and down keys, this exhibits this scroll behavior - when the cursor is at the very top and I press up (and vice versa), it scrolls the document half the page, that is, the current line is now in the middle of the text box.

Can this behavior be disabled? Is it possible to scroll it one line at a time? So, is the current line always at the top (or bottom)?

+6
source share
2 answers

I know I'm a little late to the party, but since I didn’t answer it properly, I will try. I had the same problem with NSTextViews. When using -scrollRangeToVisible to bring the insertion position to the visible rectangle of the view, this habit had scrolling to place the caret in the (vertical) middle of the screen. Instead, I used [NSView scrollRectToVisible] as it scrolls the minimum distance necessary to bring the rectangle into the visible rectangle 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 this helps!

+1
source

NSTextView (via NSText) has a method - scrollRangeToVisible:. Consider a subclass of NSTextView and override -scrollRangeToVisible to directly indicate your own scroll behavior.

If you need more specific help with the correct correct behavior, send a separate question, indicating exactly the behavior you want and the code that you tried to get it.

+1
source

Source: https://habr.com/ru/post/921757/


All Articles