在 Cocoa 的文本编辑控件中获取当前插入符位置的窗口坐标的最佳方法是什么?
这是我在其他平台上的做法
- Windows -
EM_POSFROMCHAR
- GTK - 使用
gtk_text_view_get_iter_location
和gtk_text_view_buffer_to_window_coords
。
我想知道如何使用 Cocoa 完成同样的操作。我在 MacOSX 10.6.8 上,我正在使用 C++ 执行此操作。
最佳答案
假设 textView
是一个指向 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;
关于c++ - 获取插入点的窗口坐标 - Cocoa,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7554472/