Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the coordinates for the selected text in UIWebView?

I have simple UIWebView with loaded html file. I want to show the PopOverController pointed to the selected text like -

enter image description here.

I want the coordinates of selected text from UIWebView. If I set the scalesPageToFit property of UIWebView to NO, then this link works fine. If I set the scalesPageToFit property of UIWebView to YES, then it fails.

Anyone please help me to sort out my problem.

like image 682
Girish Avatar asked Oct 18 '13 12:10

Girish


1 Answers

First of all remove the native long press gesture recogniser like this:

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

Then assign a custom one:

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

        // set numberOfTapsRequired and numberOfTouchesRequired as per your requirement:       

[yourWebView addGestureRecognizer:myOwnLongPressRecog];

// Handle Long press like this:

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

            }
like image 90
nr5 Avatar answered Oct 23 '22 08:10

nr5