gpt4 book ai didi

javascript - 如何获取UIWebView中选中文本的坐标?

转载 作者:技术小花猫 更新时间:2023-10-29 10:53:30 25 4
gpt4 key购买 nike

我有一个简单的 UIWebView 加载了 html 文件。我想显示 PopOverController 指向所选文本,如 -

enter image description here .

我想要从 UIWebView 中选择的文本的坐标。如果我将 UIWebViewscalesPageToFit 属性设置为 NO,则 this链接工作正常。如果我将 UIWebViewscalesPageToFit 属性设置为 YES,则会失败。

任何人请帮助我解决我的问题。

最佳答案

首先像这样移除原生的长按手势识别器:

for(UIGestureRecognizer *gesRecog in yourWebView.gestureRecognizers)
{
if([gesRecog isKindOfClass:[UILongPressGestureRecognizer class]])
{
[startTF removeGestureRecognizer:gesRecog];
}
}

然后指定一个自定义的:

    UILongPressGestureRecognizer *myOwnLongPressRecog = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handleWebViewLongpress:)];

// set numberOfTapsRequired and numberOfTouchesRequired as per your requirement:

[yourWebView addGestureRecognizer:myOwnLongPressRecog];

//像这样处理长按:

 - (void) handleWebViewLongpress: (UIGestureRecognizer *) recog
{
int zoomedWidth = [[yourWebView stringByEvaluatingJavaScriptFromString:@"window.innerWidth"] intValue];

CGFloat scale = yourWebView.frame.size.width / zoomedWidth; // get the scaled value of your web view

CGPoint zoomedCords = [gesture locationInView:self.webView];

zoomedCords.x /= scale; // Normal math. Divide by the scale to get the real thing.
zoomedCords.y /= scale;

NSLog(@"%@", zoomedCords);

}

关于javascript - 如何获取UIWebView中选中文本的坐标?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19449680/

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