获取插入点的窗口坐标 - Cocoa

发布于 2024-12-06 19:14:47 字数 319 浏览 0 评论 0原文

在 Cocoa 的文本编辑控件中获取当前插入符位置的窗口坐标的最佳方法是什么?

以下是我在其他平台

  1. Windows - EM_POSFROMCHAR
  2. GTK - 使用 gtk_text_view_get_iter_locationgtk_text_view_buffer_to_window_coords 中执行此操作的方法。

我想知道如何使用 Cocoa 来完成同样的事情。我使用的是 MacOSX 10.6.8,并且使用 C++ 执行此操作。

What is the best way to get window coordinates of the current caret position in a text edit control in Cocoa?

Here is how I do that in other platforms

  1. Windows - EM_POSFROMCHAR
  2. GTK - Using gtk_text_view_get_iter_location and gtk_text_view_buffer_to_window_coords.

I am wondering how the same can be done using Cocoa. I am on MacOSX 10.6.8 and I am doing this using C++.

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

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

发布评论

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

评论(2

短暂陪伴 2024-12-13 19:14:47

假设 textView 是一个指向文本视图的变量,而 window 是一个指向窗口的变量:

// -firstRectForCharacterRange:actualRange returns a frame rectangle
// containing the insertion point or, in case there's a selection,
// the first line of that selection. The origin of the frame rectangle is
// in screen coordinates
NSRange range = [textView selectedRange];
NSRect screenRect = [textView firstRectForCharacterRange:range actualRange:NULL];

// -convertRectFromScreen: converts from screen coordinates
// to window coordinates
NSRect windowRect = [window convertRectFromScreen:screenRect];

// The origin is the lower left corner of the frame rectangle
// containing the insertion point
NSPoint insertionPoint = windowRect.origin;

Assuming textView is a variable that points to the text view and window is a variable that points to the window:

// -firstRectForCharacterRange:actualRange returns a frame rectangle
// containing the insertion point or, in case there's a selection,
// the first line of that selection. The origin of the frame rectangle is
// in screen coordinates
NSRange range = [textView selectedRange];
NSRect screenRect = [textView firstRectForCharacterRange:range actualRange:NULL];

// -convertRectFromScreen: converts from screen coordinates
// to window coordinates
NSRect windowRect = [window convertRectFromScreen:screenRect];

// The origin is the lower left corner of the frame rectangle
// containing the insertion point
NSPoint insertionPoint = windowRect.origin;
聽兲甴掵 2024-12-13 19:14:47

以下方法位于文档

- (void)drawInsertionPointInRect:(NSRect)aRect color:(NSColor *)aColor returnedOn:(BOOL)flag 中,

它说

调用此方法时,焦点必须锁定在接收者上。您不需要直接调用此方法。

如果您在 NSTextView 子类中重写此方法(并调用超级实现),它将允许您知道插入点的位置,至少在 Cocoa 需要时是这样。

The following method is in the documentation

- (void)drawInsertionPointInRect:(NSRect)aRect color:(NSColor *)aColor turnedOn:(BOOL)flag

it says

The focus must be locked on the receiver when this method is invoked. You should not need to invoke this method directly.

If you override this method (and call the super implementation) in an NSTextView subclass it would allow you to know the position of the insertion point, at least when it's required by Cocoa.

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