4
我有一个UITextView挂钩到一个平移手势识别器。当我将手指拖到文本视图上时,我使用characterIndexForPoint方法来确定手指在哪个字符上方&,然后突出显示该点处的文本。characterIndexForPoint返回不同的结果为相同的参数
在某些情况下,视图中的文本有换行符,即使传入相同的参数,此方法似乎会返回不同的结果。被触摸的方法连续调用两次,一旦charIndex是167,然后是270.
我已经检查了文本视图的AttributedString,它在两种情况下都是相同的字体大小。
- (IBAction)touched:(UIPanGestureRecognizer *)sender {
if (self.txtView.isFirstResponder) {
return;
}
if (sender.state == UIGestureRecognizerStateChanged) {
CGPoint touchPoint = [sender locationInView:self.txtView];
NSUInteger charIndex = [self.txtView.layoutManager
characterIndexForPoint:touchPoint
inTextContainer:self.txtView.textContainer
fractionOfDistanceBetweenInsertionPoints:0];
...
这是一个已知的错误吗?难道我做错了什么?
'触摸的方法连续调用两次,一旦charIndex是167,然后是270.' - 是两个调用中使用的相同'touchPoint'吗? –
是的,这是(这就是为什么我写“传递相同的参数”) – Epaga