iOS检测点击字符

时间:2014-08-26 14:26:47

标签: ios iphone ipad nslayoutmanager

你好我给了一个字符串:

[102,103] [207]

我已将TapGestureRecognizer添加到字符串中,并希望检测用户点击的字符,这是我的代码:

    - (void)handleTapFrom: (UITapGestureRecognizer *)recognizer {

if (recognizer.state == UIGestureRecognizerStateEnded) {
    CGPoint location = [recognizer locationInView:self];

    NSTextStorage *textStorage = [[NSTextStorage alloc] initWithAttributedString:self.attributedText];
    NSLayoutManager *layoutManager = [[NSLayoutManager alloc] init];
    [textStorage addLayoutManager:layoutManager];


    NSTextContainer *textContainer = [[NSTextContainer alloc] initWithSize:self.bounds.size];
    [layoutManager addTextContainer:textContainer];

    textContainer.maximumNumberOfLines = self.numberOfLines;
    textContainer.lineBreakMode = self.lineBreakMode;

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

始终返回最后一个字符的索引(在这种情况下= 14) - 为什么?

0 个答案:

没有答案