简体   繁体   English

在UITextView中从tap中获取单词

[英]Get word from tap in UITextView

I'm trying to get a word from a tap. 我正试着从水龙头里得到一个字。 The following function's sender is from the UIGesture on the UITextView , sentence. 以下函数的sender来自UITextView上的UIGesture ,句子。

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

This is what I already have, but it prints the word out as NULL 这是我已经拥有的,但它将单词输出为NULL

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM