如何在UIWebView中获取所选文本的坐标?

Gir*_*ish 11 javascript iphone uiwebview ios

我有简单UIWebView的加载html文件.我想显示PopOverController指向所选文本的像 -

在此输入图像描述.

我想要coordinates选择的文字UIWebView.如果我将scalesPageToFit属性设置UIWebViewNO,则链接正常工作.如果我将scalesPageToFit属性设置UIWebViewYES,则失败.

有人请帮我解决我的问题.

nr5*_*nr5 1

首先删除本机长按手势识别器,如下所示:

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

然后指定一个自定义的:

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

        // set numberOfTapsRequired and numberOfTouchesRequired as per your requirement:       

[yourWebView addGestureRecognizer:myOwnLongPressRecog];
Run Code Online (Sandbox Code Playgroud)

// 像这样处理长按:

 - (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);

            }
Run Code Online (Sandbox Code Playgroud)