在Cocoa的文本编辑控件中获取当前插入符号位置的窗口坐标的最佳方式是什么?获取插入点的窗口坐标 - Cocoa
这是我如何做,在其他平台
- 的Windows -
EM_POSFROMCHAR
- GTK - 使用
gtk_text_view_get_iter_location
和gtk_text_view_buffer_to_window_coords
。
我想知道如何使用可可做同样的事情。我在MacOSX 10.6.8上,我正在使用C++进行此操作。
在Cocoa的文本编辑控件中获取当前插入符号位置的窗口坐标的最佳方式是什么?获取插入点的窗口坐标 - Cocoa
这是我如何做,在其他平台
EM_POSFROMCHAR
gtk_text_view_get_iter_location
和gtk_text_view_buffer_to_window_coords
。我想知道如何使用可可做同样的事情。我在MacOSX 10.6.8上,我正在使用C++进行此操作。
假设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;
以下的方法是在文档中
- (void)drawInsertionPointInRect:(NSRect)aRect color:(NSColor *)aColor turnedOn:(BOOL)flag
它说
重点必须当这个方法被调用时被锁定在接收器上。你不应该直接调用这个方法。
如果您在NSTextView子类中重写此方法(并调用超级实现),它将允许您知道插入点的位置,至少在Cocoa需要时。
我会对NSTextView不感兴趣。感谢您查看这个。 –
还有一个问题:你已经将问题标记为C++,但Cocoa主要提供Objective-C API。 Objective-C(或Objective-C++)是一种可行的解决方案还是只对C++感兴趣? – 2011-09-30 02:38:16
任何关于如何解决问题的想法都会很好。我可以解决语言问题。再次感谢。 –