【发布时间】:2012-10-14 17:07:42
【问题描述】:
我想从水龙头里得到一个字。以下函数的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]);
}
这是我已经拥有的,但它会将单词打印为 NULL
【问题讨论】:
-
尝试将 pos 更改为 pos = [sender locationInView:_tv] 然后移除 pos.y 调整。
-
哦,你真是个天才!非常感谢!!
标签: objective-c ios