gpt4 book ai didi

objective-c - 在 UITextView 中长按获取单词

转载 作者:可可西里 更新时间:2023-11-01 04:11:34 30 4
gpt4 key购买 nike

现在我已经在 UITextView 中检测到长按

    - (void)viewDidLoad
{
[super viewDidLoad];
UILongPressGestureRecognizer *LongPressgesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handleLongPressFrom:)];
[[self textview] addGestureRecognizer:LongPressgesture];
longPressGestureRecognizer.delegate = self;
}
- (void) handleLongPressFrom: (UISwipeGestureRecognizer *)recognizer
{
CGPoint location = [recognizer locationInView:self.view];

NSLog(@"Tap Gesture Coordinates: %.2f %.2f", location.x, location.y);
}

现在,我应该如何获取长按的单词的内容,并获取该单词的矩形以准备显示 PopOver?

最佳答案

此函数将返回 UITextView 中给定位置的单词。

+(NSString*)getWordAtPosition:(CGPoint)pos inTextView:(UITextView*)_tv
{
//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];

return [_tv textInRange:wr];
}

关于objective-c - 在 UITextView 中长按获取单词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11349459/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com