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!
source share