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

Javascript 如何在UIWebView中获取所选文本的坐标?,javascript,iphone,ios,uiwebview,Javascript,Iphone,Ios,Uiwebview,我有一个简单的UIWebView和加载的html文件。我想显示指向所选文本的PopOverController,如- 我需要UIWebView中选定文本的坐标。 如果我将UIWebView的scalesPageToFit属性设置为NO,则链接工作正常。如果我将UIWebView的scalesPageToFit属性设置为YES,那么它将失败 请任何人帮我解决我的问题 首先删除本机长按手势识别器,如下所示: for(UIGestureRecognizer *gesRecog in yourWebV

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

我需要
UIWebView
中选定文本的
坐标。
如果我将
UIWebView
scalesPageToFit
属性设置为
NO
,则链接工作正常。如果我将
UIWebView
scalesPageToFit
属性设置为
YES
,那么它将失败


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

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

for(UIGestureRecognizer *gesRecog in yourWebView.gestureRecognizers)
    {
        if([gesRecog isKindOfClass:[UILongPressGestureRecognizer class]])
        {
            [startTF removeGestureRecognizer:gesRecog];
        }
    }
 - (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);

            }
NSString *rectsString = [webView stringByEvaluatingJavaScriptFromString:@"rectsForSelection();"];
NSData *rectsData = [rectsString dataUsingEncoding:NSUTF8StringEncoding];
NSArray *rects = [NSJSONSerialization JSONObjectWithData:rectsData
                                                 options:0
                                                   error:NULL]; //Do Your Own Error Checking
然后指定一个自定义的:

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

        // set numberOfTapsRequired and numberOfTouchesRequired as per your requirement:       

[yourWebView addGestureRecognizer:myOwnLongPressRecog];
//长按如下方式操作:

for(UIGestureRecognizer *gesRecog in yourWebView.gestureRecognizers)
    {
        if([gesRecog isKindOfClass:[UILongPressGestureRecognizer class]])
        {
            [startTF removeGestureRecognizer:gesRecog];
        }
    }
 - (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);

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

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实际上会将HTML呈现到UIView中,特别是它可能会将可选文本呈现到UITextView中,因此您要做的是尝试使用右视图的委托方法

下面是一个应该有效的黑客:

  • 等待web视图完全加载
  • 按照Ole Begemann的描述遍历UIWebView的子视图
  • 一旦到达UITextView,点击其委托方法,您可以执行类似于委托链的操作,以获得以下信息-
  • 实现
    UITextViewDelegate
    方法
    textViewDidChangeSelection:
    以获取selectedRange并与之交互

  • **第3步和第4步的替代方法是使用KVO来监听textView的selectedRange中的更改。

    这必须几乎完全在JavaScript中完成,然后将结果传递回Objective C。如果您可以控制显示的内容,则可以将此函数添加到
    标记中。否则,您将需要按照中所述注入它


    我要补充的是,这将获得在
    网络视图中有效的坐标。滚动视图
    ,而不是
    网络视图

    请详细解释您的问题。@virantporwal为了更好地理解,我编辑了我的问题。这里的“rect”是什么意思?好的,然后您可以尝试使用TapUIgestureRecognitor获取微粒坐标。@Girish Ohh抱歉,老板……我已经尝试过了,但由于scalesPageToFit属性,它返回了错误的y坐标。在缩放时更改您的y坐标。如何更改y坐标?用户可以从UIWebView中选择任何文本。您必须获得缩放值。比如当你缩放一幅图像,想要得到用户实际点击的位置。检查我的最新答案。你之前说过,它不起作用。以前怎么了??万一我遇到同样的问题。。。