12

我试图从水龙头里得到一个字。以下函数sender来自UIGestureon the UITextView, 句子。

- (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 回答