在NSTextView中拖动插入符与characterIndexForPoint的结果不匹配 [英] Dragging caret in NSTextView doesn't match result of characterIndexForPoint

查看:52
本文介绍了在NSTextView中拖动插入符与characterIndexForPoint的结果不匹配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm dragging onto an NSTextView (from a table in another window) and as I drag over the text in the NSTextView a caret is displayed showing the insertion point. There appears to be no way to get the location of this caret and I'm using:

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];

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

The problem is that sometimes the inserted text is being inserted one character to the left of the displayed caret. I've tried different values for 'fractionOfDistanceBetweenInsertionPoints' without success.

第一季度。是否有一种方法可以获取拖动的插入符号的位置?

Q1. Is there a way to get the position of the dragging caret?

Q2。苹果公司在使用什么 fractionOfDistanceBetweenInsertionPoints值?

Q2. What 'fractionOfDistanceBetweenInsertionPoints' value is Apple using?

第三季度。我应该使用其他方法(也许是drawInsertionPointInRect)吗?

Q3. Should I be using an alternative approach (drawInsertionPointInRect perhaps)?

谢谢。

推荐答案

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

Ok, the solution is to use characterIndexForInsertionAtPoint instead of characterIndexForPoint. Thusly,

NSPoint mouseLocation = [sender draggingLocation];
NSUInteger dropLocation = [self characterIndexForInsertionAtPoint:[self convertPoint:mouseLocation fromView:nil]];

这篇关于在NSTextView中拖动插入符与characterIndexForPoint的结果不匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆