Disable WebView magnifier and copy-paste pop-up box

Source: Internet
Author: User

Wen/kyxu (author of Jane's book)
Original link: http://www.jianshu.com/p/40048d9c979a
Copyright belongs to the author, please contact the author to obtain authorization, and Mark "book author".

Background:
When you have WebView or text in your app, there's no question that the system will pop up a box to copy, paste, cut text, or pop up a magnifying glass by default when you press and hold.
And when you're developing, it's often clear what your WebView wants to show, and you might want to customize the long-press gesture (longpressgesture) instead of letting the system "intelligently" help you.
However, when you have done a series of work customizing the long-press gesture, you find that it has a lower priority than the default pop-up box on the system.

After looking at StackOverflow above, find a way that might work for someone:

webView.stringByEvaluatingJavaScriptFromString("document.documentElement.style.webkitUserSelect=‘none‘;")webView.stringByEvaluatingJavaScriptFromString("document.documentElement.style.webkitTouchCallout=‘none‘;")

Add these two lines of code to the Webviewshouldloadwithrequest method, and you can modify the Web page by using CSS.

Focus

However, this is not effective in all cases, even today in the Xcode 7 GM + IOS 9 environment, completely ineffective. After a long struggle, I found


Uilongpressgesturerecognizer.png

In the default property of the long-press gesture, there is a minimumpressduration default value of 0.5, which means that In the long press of 0.5 seconds, our custom long-press gestures and system conflicts, so the solution is obvious: the custom long-press gesture to reduce the response time to 0.5s, so that the system method can be done before the execution, and in fact, you can even simply rough to change it to 0.1s, still do not and tap Tap gestures are conflicting.

self.longPressPan.minimumPressDuration = 0.3

Hope to help you:)

Update: When you drag a gesture control to Viewcontroller in StoryBoard, do not drag to the top of the Viewcontroller, and drag to the Viewcontroller View section to automatically set the control area of the gesture. Otherwise, the gesture will not work regardless of how it is set.

Uilongpressgesturerecognizer *longpress = [[Uilongpressgesturerecognizer alloc] initwithtarget:self Action:nil];

Longpress.delegate = self;

Longpress.minimumpressduration = 0.3; Why set 0.3 here, because as long as the greater than 0.5 is invalid, I like probably because the default step out of the magnifying glass gesture of the long-time is 0.5 seconds,

If our custom gesture is greater than or less than 0.5 seconds, it's too late to replace his default gesture, which is just my guess. But better than 0.2.

Seconds, because some PDFs have a bookmark jump function, which is too small to invalidate these features.

[Self.webview addgesturerecognizer:longpress];

#pragma mark-uigesturerecognizerdelegate

-(BOOL) Gesturerecognizer: (Uigesturerecognizer *) Gesturerecognizer Shouldrecognizesimultaneouslywithgesturerecognizer: (Uigesturerecognizer *) Othergesturerecognizer

{

return NO; Be sure to return NO here, as to why everyone should take a look at the documentation for this method.

There is this delegate will be called multiple times when you press the NSLog, you can use the output Gesturerecognizer and Othergesturerecognizer

Let's see what it is.

}

Disable WebView magnifier and copy-paste pop-up box

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.