【问题标题】:How to get the coordinates for the selected text in UIWebView?如何获取 UIWebView 中选定文本的坐标?
【发布时间】:2013-10-27 07:09:36
【问题描述】:

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

我想要来自UIWebView 的选定文本的coordinates。 如果我将UIWebViewscalesPageToFit 属性设置为NO,那么this 链接可以正常工作。如果我将UIWebViewscalesPageToFit 属性设置为YES,则会失败。

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

【问题讨论】:

  • 请详细说明您的问题。
  • @virantporwal 我已经编辑了我的问题以便更好地理解。
  • 这里的“rect”是什么意思?
  • OK,那你可以试试 TapUIGestureRecognizer 来获取粒子坐标。
  • @Girish Ohh sorry boss...

标签: javascript iphone ios uiwebview


【解决方案1】:

这必须几乎完全在 JavaScript 中完成,然后将结果传递回 Objective C。如果您可以控制所显示的内容,则可以将此函数添加到 <script> 标记中。否则,您需要按照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);
}

然后从您的 Objective C 代码中,您使用执行以下操作:

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

我要补充一点,这将获得在您的webView.scrollView 内有效的坐标,而不是您的webView

【讨论】:

    【解决方案2】:

    UIWebView 实际上将 HTML 渲染到 UIViews 中,特别是它可能会将可选择的文本渲染到 UITextView 中,所以你要做的就是尝试进入正确的视图的委托方法。

    这是一个应该有效的技巧:

    1. 等到 Web 视图完全加载。
    2. 遍历 UIWebView 的子视图,如 Ole Begemann here 所述。
    3. 一旦你到达 UITextView 并点击它的委托方法,你可以做一些类似于委托链的事情来获得这个 - here's a post about it
    4. 实现 UITextViewDelegate 方法 textViewDidChangeSelection: 以获取 selectedRange 并与之交互。

    ** 步骤 3 和 4 的替代方法是使用 KVO 来监听 textView 的 selectedRange 的变化。

    【讨论】:

      【解决方案3】:

      你试过了吗?

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

      【讨论】:

        【解决方案4】:

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

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

        【讨论】:

        • 我已经尝试过了,但是由于 scalesPageToFit 属性,它返回了错误的 y 坐标。
        • 在缩放发生时更改 Y 坐标。
        • 如何更改 Y 坐标?用户可以从 UIWebView 中选择任何文本。
        • 你必须得到缩放值。就像您缩放图像并想要获得用户实际点击的位置一样。检查我更新的答案。
        • 你之前说过,它不起作用。之前有什么问题??以防万一我遇到同样的问题...
        猜你喜欢
        • 2011-11-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-12-16
        • 2011-02-06
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多