Pregunta

I want the user to be able to scroll an NSTextView so that the last line of text is at the top of the view. By default, an NSTextView with more than a page of text will only let the user scroll until the last line of text is at the bottom of the view. How can I accomplish this?

¿Fue útil?

Solución

Look into subclassing NSLayoutManager and assigning your subclass to the NSTextView in question. It should be a pretty simple manner to detect when it’s been asked to layout the last line of text, and return a much taller rect. You might even be able to use:

- (void)setExtraLineFragmentRect:(NSRect)fragmentRect usedRect:(NSRect)usedRect textContainer:(NSTextContainer *)container;

// Sets the bounds and container for the extra line fragment.  The extra line fragment is used when the text backing ends with a hard line break or when the text backing is totally empty, to define the extra line which needs to be displayed at the end of the text.  If the text backing is not empty and does not end with a hard line break, this should be set to NSZeroRect and nil.  Line fragment rects and line fragment used rects are always in container coordinates.

You might also try subclassing NSTextView’s sizing methods, and just adding space in them. Like, try subclassing -setFrameSize: and see what happens when you add (the height of the scrollView’s content area) - (height of the last line of text) to it and call super.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top