如何检测 UIWebView 何时开始选择文本? [英] how to detect when UIWebView starts selecting text?

查看:29
本文介绍了如何检测 UIWebView 何时开始选择文本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 UIWebView,它显示静态内容并通过其控制器对各种滑动手势做出反应.

I have a UIWebView which shows static content and reacts to various swipe gestures through its controller.

文本选择已被禁用,但现在我正在开发一项新功能,以允许选择文本并显示自定义上下文菜单.

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.

我能想到的唯一方法是在滑动处理程序方法中使用JS检查是否有选定的文本.还有其他想法吗?有人知道如何更好地做到这一点吗?

The only method I can think of is to check if there is selected text using JS in the swipe handler methods. Any other ideas? Does anybody know how to do this better?

谢谢.

推荐答案

我最终使用了以下方法来禁用和启用使用 Rangy 库.效果很好.

I ended up using the following method to disable and enable the gesture recognizers using the Rangy library. Works great.

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

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

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

这篇关于如何检测 UIWebView 何时开始选择文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆