将 NSTextView 滚动到精确的矩形?

发布于 2024-08-13 08:58:28 字数 387 浏览 4 评论 0原文

我有一个 NSTextView,位于 NSScrollView 内,我想在用新数据重新填充 NSTextView 后将其滚动到原来的位置。到目前为止,我已经尝试过下面代码的变体,但它从来没有完全正确。

我需要一个 setDocumentVisibleRect 方法,但没有。

NSRect oldVisibleRect = [[[self scrollView] contentView] documentVisibleRect];

[重新填充数据的代码]

[[[self scrollView] contentView] scrollToPoint:newPoint];

有什么想法吗?

I've got an NSTextView, inside an NSScrollView, and I want to scroll it to exactly where it used to be after repopulated the NSTextView with new data. So far, I've tried variations on the code below, but it never gets it quite right.

What I need is a setDocumentVisibleRect method, but there isn't one.

NSRect oldVisibleRect = [[[self scrollView] contentView] documentVisibleRect];

[Code to repopulate data]

[[[self scrollView] contentView] scrollToPoint:newPoint];

Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

波浪屿的海角声 2024-08-20 08:58:28

你需要更具体——这不是“完全正确”吗?

-[NSView scrollRectToVisible:]

将接收器最近的祖先 NSClipView 对象滚动所需的最小距离,以便接收器的指定区域在剪辑视图中可见。

NSTextView 是 NSView 的子类(尽管您应该始终考虑给定视图是否翻转了坐标...),因此您可以使用 -scrollRectToVisible:

另外,除非您的文本视图内容更改与以前几乎完全相同,否则旧的可见的矩形不会完全相同,所以我不希望它表现得完美。如果您仅将属性(如语法突出显示)应用于文本,则无需对文本视图(文本存储或容器)执行任何需要完全重新加载(和重新滚动)的操作。我想这可以归结为“你到底想做什么?”

You'll need to be more specific - what about this isn't "quite right"?

-[NSView scrollRectToVisible:]

Scrolls the receiver’s closest ancestor NSClipView object the minimum distance needed so a specified region of the receiver becomes visible in the clip view.

NSTextView is a subclass of NSView (though you should always consider whether a given view has flipped coordinates...), so you can use -scrollRectToVisible:

Also, unless your text view content changes are pretty much exactly the same as before, the old visible rect isn't going to be quite the same, so I wouldn't expect it to behave perfectly. If you're only applying attributes (like syntax highlighting) to the text, there'd be no need to do anything to the text view (the text storage or container) that should require a complete reload (and re-scroll). I guess it boils down to "what exactly are you trying to do?"

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文