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

时间:2013-10-18 12:27:48

标签: javascript iphone ios uiwebview

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

enter image description here

我想要coordinates中所选文字的UIWebView。 如果我将scalesPageToFit的{​​{1}}属性设置为UIWebView,则this链接可以正常工作。如果我将NO的{​​{1}}属性设置为scalesPageToFit,则会失败。

任何人都可以帮我解决我的问题。

4 个答案:

答案 0 :(得分:1)

首先删除原生的长按手势识别器,如下所示:

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

            }

答案 1 :(得分: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 :(得分:0)

你尝试过吗?

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

}

答案 3 :(得分:0)

UIWebView实际上将HTML呈现为UIViews,特别是它可能会将可选文本呈现为UITextView,因此您需要尝试使用右侧视图的委托方法。

这是一个应该有效的黑客攻击:

  1. 等到Web视图完全加载。
  2. 按照Ole Begemann here的描述遍历UIWebView的子视图。
  3. 一旦你到UITextView进入其委托方法,你可以做一些类似于委托链的事情来获得这个 - here's a post about it
  4. 实施UITextViewDelegate方法textViewDidChangeSelection:以获取selectedRange并与之互动。
  5. **步骤3和4的替代方法是使用KVO来监听textView的selectedRange中的更改。