最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - how to detect when UIWebView starts selecting text? - Stack Overflow

programmeradmin1浏览0评论

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.

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?

Thanks.

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.

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?

Thanks.

Share Improve this question asked Mar 8, 2012 at 17:22 Engin KurutepeEngin Kurutepe 6,7673 gold badges36 silver badges64 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 6

when the user select a text the menuViewController will shown up .. you can detect that by add a notification on your 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. 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. Works great.

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

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

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

评论列表(0)

  1. 暂无评论