在UITextView中从tap中获取单词

KBo*_*in9 12 objective-c ios

我正试着从水龙头里得到一个字.以下函数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]);
}
Run Code Online (Sandbox Code Playgroud)

这是我已经拥有的,但它将单词输出为NULL

KBo*_*in9 10

"尝试将pos更改为pos = [sender locationInView:_tv],然后删除pos.y调整. - rmaddy"