Macos 如何允许滚动到NSTextView的末尾?

Macos 如何允许滚动到NSTextView的末尾?,macos,cocoa,nstextview,Macos,Cocoa,Nstextview,我希望用户能够滚动NSTextView,以便最后一行文本位于视图顶部。默认情况下,包含多页文本的NSTextView只允许用户滚动,直到最后一行文本位于视图底部。我如何才能做到这一点?研究NSLayoutManager的子类化,并将您的子类分配给相关的NSTextView。它应该是一种非常简单的方式来检测何时被要求布局最后一行文本,并返回一个更高的rect。您甚至可以使用: - (void)setExtraLineFragmentRect:(NSRect)fragmentRect usedRec

我希望用户能够滚动NSTextView,以便最后一行文本位于视图顶部。默认情况下,包含多页文本的NSTextView只允许用户滚动,直到最后一行文本位于视图底部。我如何才能做到这一点?

研究NSLayoutManager的子类化,并将您的子类分配给相关的NSTextView。它应该是一种非常简单的方式来检测何时被要求布局最后一行文本,并返回一个更高的rect。您甚至可以使用:

- (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.

您还可以尝试将NSTextView的大小调整方法子类化,并在其中添加空间。比如,尝试子类化-setFrameSize:看看当您向其中添加(scrollView内容区域的高度)-(最后一行文本的高度)并调用super时会发生什么。

谢谢,我将尝试这两种方法。增加
fragmentRect
的大小不会导致滚动区域被扩展。增加
usedRect
,但也会导致光标画得非常大。:)好的,我更新了我的答案,下一步我会尝试。啊!实际上,我刚刚尝试重写
-frame
getter。它几乎有正确的行为,但也有不一致的地方导致内容跳转。我将尝试覆盖setter。我还在NSClipView中尝试覆盖
约束边界rect:
。如果覆盖
-setFrame:
-setFrameSize:
则有效。非常感谢。