【发布时间】:2014-01-25 22:03:20
【问题描述】:
我希望用户能够滚动 NSTextView,以便 最后一行文本位于视图顶部。默认情况下,超过一页文本的 NSTextView 只会让用户滚动,直到最后一行文本位于视图底部。我怎样才能做到这一点?
【问题讨论】:
标签: macos cocoa nstextview
我希望用户能够滚动 NSTextView,以便 最后一行文本位于视图顶部。默认情况下,超过一页文本的 NSTextView 只会让用户滚动,直到最后一行文本位于视图底部。我怎样才能做到这一点?
【问题讨论】:
标签: macos cocoa nstextview
查看子类 NSLayoutManager 并将您的子类分配给有问题的 NSTextView。它应该是一种非常简单的方式来检测何时要求布局最后一行文本,并返回一个更高的矩形。您甚至可以使用:
- (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。它几乎具有正确的行为,但存在导致内容跳跃的不一致之处。我会尝试覆盖设置器。我也在尝试在 NSClipView 中覆盖 constrainBoundsRect:。
-setFrame: 和 -setFrameSize:,它会起作用。谢谢!