简体   繁体   English

如何检测UIWebView何时开始选择文本?

[英]how to detect when UIWebView starts selecting text?

I have a UIWebView which shows static content and reacts to various swipe gestures through its controller. 我有一个UIWebView ,它显示静态内容并通过其控制器对各种滑动手势做出反应。

The text selection has been disabled but now I am working on a new feature to allow text selection and present a custom context menu. 文本选择已被禁用,但是现在我正在开发一项新功能,以允许选择文本并显示自定义上下文菜单。

Now I need to be able to detect when the text selection is active so that I can turn off the swipe gestures. 现在,我需要能够检测到何时选择了文本,以便可以关闭滑动手势。

The only method I can think of is to check if there is selected text using JS in the swipe handler methods. 我能想到的唯一方法是在滑动处理程序方法中使用JS检查是否有选定的文本。 Any other ideas? 还有其他想法吗? Does anybody know how to do this better? 有人知道如何做得更好吗?

Thanks. 谢谢。

when the user select a text the menuViewController will shown up .. you can detect that by add a notification on your viewController 当用户选择文本时,menuViewController将显示..您可以通过在viewController上添加通知来检测到该文本

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(menuShown) name:UIMenuControllerDidShowMenuNotification object:nil];

same case to detect for the menu hide. 检查菜单隐藏的相同情况。

You may be able to use a "Long Press Gesture Recognizer" to bind to the UIWebView. 您可能可以使用“长按手势识别器”绑定到UIWebView。 You can calibrate it to the timing required for a user to press and hold on a text before the text is selected. 您可以将其校准为用户在选择文本之前按住文本所需的时间。

I ended up using the following method to disable and enable the gesture recognizers using the Rangy library. 我最终使用以下方法使用Rangy库禁用和启用了手势识别器。 Works great. 效果很好。

- (BOOL) isSelectionActive
{
    NSString * resultStr = [self.contentView stringByEvaluatingJavaScriptFromString:
                            @"rangy.getSelection().isCollapsed"];

    NSLog(@"resultStr: %@", resultStr );

    if ([resultStr isEqualToString:@"true"]) {
        return NO;
    }
    else {
        return YES;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM