使用singleTap在UIWebView中选择文本

时间:2013-10-15 05:45:40

标签: iphone uiwebview uigesturerecognizer uimenucontroller uimenuitem

我有UIWebView用于显示文章。这些都是HTML页面。我需要从文章中选择文字。所以,我正在使用UIMenucontroller作为选择选项。我需要在用户点击singleTap时选择文字。但是当使用singleTap时没有任何事情发生。

-(void)ViewDidLoad{

UITapGestureRecognizer *singletapp=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(singleTap:)];
    singletapp.numberOfTapsRequired=1;

    [wbCont addGestureRecognizer:singletapp];

}

SingleTap:

-(void)singleTap:(UIGestureRecognizer *)gestureRecognizer
 {

     NSLog(@"single tap");

 CGPoint touchPoint = [gestureRecognizer locationInView:self.view];

 NSString *js = [NSString stringWithFormat:@"document.elementFromPoint(%f, %f).toString()", touchPoint.x, touchPoint.y];

 NSString * tagName = [wbCont stringByEvaluatingJavaScriptFromString:js];

 NSLog(@"Selected Name: %@",tagName);

 }

1 个答案:

答案 0 :(得分:0)

在viewDidLoad中添加此代码

[[NSNotificationCenter defaultCenter] addObserver:self
                                                 selector:@selector(resetMenuController)
                                                     name:UIMenuControllerDidHideMenuNotification
                                                   object:nil];

在您的班级中添加此代码。控件将不会显示

- (void) resetMenuController
{
    UIMenuItem *notesMI = [[UIMenuItem alloc] initWithTitle: @""
                                                     action: @selector(sample)];

    [[UIMenuController sharedMenuController] setMenuItems:@[notesMI]];
}
- (BOOL)canPerformAction:(SEL)action withSender:(id)sender
{
    if (action == @selector(sample))
    {
        return YES;
    }
    return NO;
}

- (void) sample{}