找到了解决方案。
子类 UITextView 并添加以下内容:
- (UITextPosition *)closestPositionToPoint:(CGPoint)point
{
point.y -= self.textContainerInset.top;
point.x -= self.textContainerInset.left;
CGFloat fraction = 1;
NSUInteger glyphIndex = [self.layoutManager glyphIndexForPoint:point inTextContainer:self.textContainer fractionOfDistanceThroughGlyph:&fraction];
NSInteger index = glyphIndex;
if (![[self.text substringFromIndex:self.text.length - 1] isEqualToString:@"\n"]) {
if (index == [self.text length] - 1 && roundf(fraction) > 0) {
index++;
}
}
NSUInteger characterIndex = [self.layoutManager characterIndexForGlyphAtIndex:index];
UITextPosition *pos = [self positionFromPosition:self.beginningOfDocument offset:characterIndex];
return pos;
}