0

Hello i gave a string that is:

[102,103] [207]

I have added TapGestureRecognizer to the string, and want to detect on which character user tapped, here is my code:

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

Always returns index of a last char ( in this situation = 14 ) - why?

Developer
  • 4,158
  • 5
  • 34
  • 66

0 Answers0