12

タップから単語を取得しようとしています。次の関数は、、文senderからのものです。UIGestureUITextView

- (IBAction)printWordSelected:(id)sender {
    NSLog(@"Clicked");

    CGPoint pos = [sender locationInView:self.view];
    UITextView *_tv = sentence;

    NSLog(@"Tap Gesture Coordinates: %.2f %.2f", pos.x, pos.y);

    //eliminate scroll offset
    pos.y += _tv.contentOffset.y;

    //get location in text from textposition at point
    UITextPosition *tapPos = [_tv closestPositionToPoint:pos];

    //fetch the word at this position (or nil, if not available)
    UITextRange * wr = [_tv.tokenizer rangeEnclosingPosition:tapPos withGranularity:UITextGranularityWord inDirection:UITextLayoutDirectionRight];

    NSLog(@"WORD: %@", [_tv textInRange:wr]);
}

これは私がすでに持っているものですが、それは単語をNULLとして出力します

4

1 に答える 1

10

「posをpos=[sender locationInView:_tv]に変更してから、pos.y調整を削除してみてください。–rmaddy」

于 2012-11-16T16:12:41.327 に答える