在NSTextView中拖动插入符与characterIndexForPoint的结果不匹配

psm*_*irl 3 cocoa

我正在拖动到NSTextView上(从另一个窗口的表中),并且当我在NSTextView中拖动文本时,将显示插入标记,其中显示了插入点。似乎无法获取此插入符号的位置,我正在使用:

NSPoint mouseLocation = [sender draggingLocation];
NSPoint localMouseLocation = [self convertPoint:mouseLocation fromView:nil];
CGFloat fraction = 0.4;
NSUInteger dropLocation = [[self layoutManager] characterIndexForPoint:localMouseLocation inTextContainer:[self textContainer] fractionOfDistanceBetweenInsertionPoints:&fraction];
Run Code Online (Sandbox Code Playgroud)

问题是有时插入的文本会在显示的插入符号的左侧插入一个字符。我尝试了'fractionOfDistanceBetweenInsertionPoints'的不同值,但没有成功。

Q1。有没有一种方法可以获取插入的插入符的位置?

Q2。苹果使用什么“ fractionOfDistanceBetweenInsertionPoints”值?

Q3。我是否应该使用其他方法(也许是drawInsertionPointInRect)?

谢谢。

psm*_*irl 5

好的,解决方案是使用characterIndexForInsertionAtPoint而不是characterIndexForPoint。因此,

NSPoint mouseLocation = [sender draggingLocation];
NSUInteger dropLocation = [self characterIndexForInsertionAtPoint:[self convertPoint:mouseLocation fromView:nil]];
Run Code Online (Sandbox Code Playgroud)