最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to get the coordinates for the selected text in UIWebView? - Stack Overflow

programmeradmin0浏览0评论

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

.

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.

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

.

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.

Share Improve this question edited May 23, 2017 at 12:24 CommunityBot 11 silver badge asked Oct 18, 2013 at 12:27 GirishGirish 4,7124 gold badges37 silver badges55 bronze badges 7
  • Please explain more about your issue. – virantporwal Commented Oct 18, 2013 at 12:28
  • @virantporwal I have edited my question for better understanding. – Girish Commented Oct 18, 2013 at 12:39
  • What is mean by "rect" here? – virantporwal Commented Oct 18, 2013 at 12:41
  • OK,then you can try TapUIGestureRecognizer for getting the particulate coordinates. – virantporwal Commented Oct 18, 2013 at 12:50
  • @Girish Ohh sorry boss... – Dharmbir Singh Commented Oct 18, 2013 at 12:53
 |  Show 2 more ments

4 Answers 4

Reset to default 1 +50

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

            }

This must be be acplished almost pletely within JavaScript and then have the result passed back into Objective C. If you have control of the content you are showing, you can add this function into a <script> tag. Otherwise you will need to inject it as described in this post.

function rectsForSelection() {
    var i = 0, j = 0;
    var allSelections = window.getSelection();
    var result = []; // An empty array right now
    // Generally, there is only one selection, but the spec allows multiple
    for (i=0; i < allSelections.rangeCount; i++) {
        var aRange = allSelections.getRangeAt(i);
        var rects = aRange.getClientRects();
        for (j=0; j<rects.length; j++) {
            result.push(rects[j]);
        }
    }
    return JSON.stringify(result);
}

Then from your Objective C code, you use do something like this:

NSString *rectsString = [webView stringByEvaluatingJavaScriptFromString:@"rectsForSelection();"];
NSData *rectsData = [rectsString dataUsingEncoding:NSUTF8StringEncoding];
NSArray *rects = [NSJSONSerialization JSONObjectWithData:rectsData
                                                 options:0
                                                   error:NULL]; //Do Your Own Error Checking

I'll add that this will get coordinates that are valid within your webView.scrollView, not your webView.

have you tried that?

UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(getCoordinates:)];
//  [longPress setMinimumPressDuration:1];
[yourWebView addGestureRecognizer:longPress];


- (void)getCoordinates:(UILongPressGestureRecognizer *)sender {

CGPoint location = [sender locationInView:self.view];
NSLog(@"Tap at %1.0f, %1.0f", location.x, location.y);

}

UIWebView actually renders the HTML into UIViews, specifically it will probably render the selectable text into UITextView, so what you have to do is try to tap into the right view's delegate methods.

Here's a hack that should work:

  1. Wait until the web view is fully loaded.
  2. Traverse over the UIWebView's subviews as described by Ole Begemann here.
  3. Once you reach a UITextView tap into its delegate method, you could do something similar to a delegate chain to obtain this - here's a post about it
  4. Implement the UITextViewDelegate method textViewDidChangeSelection: to get the selectedRange and interact with it.

** An alternative to step 3 and 4 is to use KVO to listen to changes in the textView's selectedRange.

发布评论

评论列表(0)

  1. 暂无评论