本文介绍了在UITextView中从tap中获取单词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正试图从水龙头中获取一个字。以下函数的 sender
来自 UITextView
上的 UIGesture
,句子。
I'm trying to get a word from a tap. The following function's sender
is from the UIGesture
on the UITextView
, 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
This is what I already have, but it prints the word out as NULL
推荐答案
尝试将pos更改为pos = [sender locationInView:_tv]然后删除pos.y调整。 - rmaddy
"Try changing pos to pos = [sender locationInView:_tv] and then remove the pos.y adjustment. – rmaddy"
这篇关于在UITextView中从tap中获取单词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!