我正在尝试通过点击获取一个词。以下函数的 sender
来自 UITextView
上的 UIGesture
, sentence.
- (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
最佳答案
“尝试将 pos 更改为 pos = [sender locationInView:_tv],然后删除 pos.y 调整。– rmaddy”
关于objective-c - 从 UITextView 中的点击获取单词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12884520/