Ios 在UITextView的属性化文本中的最后一个字符后调用链接方法

Ios 在UITextView的属性化文本中的最后一个字符后调用链接方法,ios,uitextview,nsmutableattributedstring,Ios,Uitextview,Nsmutableattributedstring,我有一个文本视图,它有一个UITapSignature,它将此方法作为选择器-(void)textTapped:(UITapGestureRecognizer*)识别器。当我点击文本视图最后一行下方或显示的最后一个字符之后的一点时,它将调用最后一个字符的属性文本上的方法,因为它似乎:layoutManager characterandexforpoint:location inTextContainer:textView.textContainer 插入点之间距离的分位数:NULL]

我有一个文本视图,它有一个UITapSignature,它将此方法作为选择器
-(void)textTapped:(UITapGestureRecognizer*)识别器
。当我点击文本视图最后一行下方或显示的最后一个字符之后的一点时,它将调用最后一个字符的属性文本上的方法,因为它似乎:
layoutManager characterandexforpoint:location
inTextContainer:textView.textContainer
插入点之间距离的分位数:NULL]characterIndex
,这样可以很好地处理这种情况,但如果我点击最后一个字符,它就不会调用函数。有没有一种方法可以防止最后一个字符在未被点击时返回,而只获得点击时的确切字符,或者有其他方法可以防止调用该方法

- (void)textTapped:(UITapGestureRecognizer *)recognizer
{
    UITextView *textView = (UITextView *)recognizer.view;

    // Location of the tap in text-container coordinates

    NSLayoutManager *layoutManager = textView.layoutManager;
    CGPoint location = [recognizer locationInView:textView];
    location.x -= textView.textContainerInset.left;
    location.y -= textView.textContainerInset.top;

    // Find the character that's been tapped on

    NSUInteger characterIndex;
    characterIndex = [layoutManager characterIndexForPoint:location
                                           inTextContainer:textView.textContainer
                  fractionOfDistanceBetweenInsertionPoints:NULL];

    if (characterIndex < textView.textStorage.length) {

        NSRange range;
        id value = [textView.attributedText attribute:@"myCustomTag" atIndex:characterIndex effectiveRange:&range];

        // Handle as required...

        NSLog(@"%@, %d, %d", value, range.location, range.length);

    }
}
-(void)text点击:(UITapGestureRecognitor*)识别器
{
UITextView*textView=(UITextView*)识别器.view;
//点击在文本容器坐标中的位置
NSLayoutManager*layoutManager=textView.layoutManager;
CGPoint位置=[识别器位置视图:文本视图];
location.x-=textView.textContainerSet.left;
location.y-=textView.textContainerSet.top;
//找到被点击的角色
NSU整数特征索引;
characterIndex=[layoutManager CharacterIndexorPoint:位置
inTextContainer:textView.textContainer
插入点之间距离的分位数:NULL];
if(characterIndex
我认为您需要使用
glyphIndexForPoint
文档,而不是使用
characterIndexForPoint

现在,在文档中,它清楚地告诉您找到标志符号后要遵循哪些步骤,我将引用文档中的段落:

如果点下没有标志符号,则返回最近的标志符号,其中 最近距离是根据用户选择的要求定义的 老鼠希望确定该点是否实际存在的客户 在返回的标志符号的范围内的,应在其后加上 调用
boundingrectforglyprange:inTextContainer:
并测试 该点位于该方法返回的矩形中。如果 partialFraction是非NULL的,它通过引用返回 返回的图示符位置与标记之间的距离 下一个图示符的位置