【问题标题】:Get word from tap in UITextView从 UITextView 中的点击获取单词
【发布时间】: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

【问题讨论】:

标签: objective-c ios


【解决方案1】:

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

【讨论】:

  • 这很好用,除非 UITextview 没有滚动。但是当我们滚动 textview 时,不会打印正确的单词。
猜你喜欢
  • 2014-10-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-02-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多