显示文档时没有复制/粘贴和选择矩形的UIWebView [英] UIWebView without Copy/Paste and selection rectangle when showing documents

查看:174
本文介绍了显示文档时没有复制/粘贴和选择矩形的UIWebView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序中,我想禁用UIWebView对象显示的内容的复制/粘贴/剪切。为实现这一目标,我创建了一个UIWebView子类并覆盖 - (BOOL)canPerformAction:(SEL)动作withSender:(id)sender 方法:

In my App I want to disable the Copy/Paste/Cut of contents that are showed by a UIWebView object. To achieve this, I created a UIWebView subclass and overrode the - (BOOL)canPerformAction:(SEL)action withSender:(id)sender method:

#pragma mark - UIResponderStandardEditActions 

- (BOOL)canPerformAction:(SEL)action withSender:(id)sender {    
    if (action == @selector(copy:) ||
        action == @selector(paste:)||
        action == @selector(cut:)) {
        return _copyCutAndPasteEnabled;
    }

    return [super canPerformAction:action withSender:sender];
}

现在用户不再可以进行此类操作,但UIWebView仍显示选择矩形,如下面的屏幕截图所示:

Now the user no longer can make such operations, however the UIWebView still shows the "selection rectangle", as you can see in the following screenshot:

注意:UIWebView中显示的内容不是HTML页面。我正在显示文档文件(PDF,DOC,PPT),使用以下文件从文件加载:

NOTE: The content being showed in the UIWebView is not HTML pages. I'm showing document files (PDF, DOC, PPT), loaded from a file using:

NSURL *fileURL = [NSURL fileURLWithPath:<document file path..>];
NSURLRequest *fileRequest = [NSURLRequest requestWithURL:fileURL];
[<uiwebView> loadRequest:fileRequest];

有没有办法禁用/隐藏这个选择矩形功能?

Is there any way to disable/hide this selection rectangle feature too?

[] s,

推荐答案

您可以尝试将javascript注入webView。此代码也适用于iPhone,但仅限于页面完全加载时。
http://www.javascriptsource.com/page-details /disable-text-selection.html
http://solidlystated.com/scripting/proper-way-to-disable-text-selection-and-highlighting/

You can try injecting javascript into the webView. This code works on the iPhone too but only when the page is fully loaded. http://www.javascriptsource.com/page-details/disable-text-selection.html or http://solidlystated.com/scripting/proper-way-to-disable-text-selection-and-highlighting/

为了使页面只有一半加载或仍在加载时你可以正常使用类似于你注入禁用javascript的设置,就像它开始选择一样。
http:// www.icab.de/blog/2010/07/11/customize-the-contextual-menu-of-uiwebview/

To get it to work properly when the page is only half loaded or still loading you'll proably have to use a setup similar to this one where you inject the disabling javascript just as it would start selecting. http://www.icab.de/blog/2010/07/11/customize-the-contextual-menu-of-uiwebview/

我能做的最后一件事想想这可能有用,你测试过将选择器@selector(select :)添加到禁用的选择器列表中吗?

One last thing I can think of that might work, have you tested adding the selector @selector(select:) to the list of disabled selectors?

编辑:
好​​吧你什么的要显示PDF而不是html页面,你可以试试这个。

Ok as you what to display a PDF rather then a html page you can try this.

设置

将webView放在scrollView中;

Put the webView in a scrollView;

设置ScrollView的:委托给自己;最小缩放到1.0并最大缩放到你想要的任何东西;

Set the ScrollView's: delegate to self; min zoom to 1.0 and max zoom to whatever you want;

weView完成加载后。
扫描webView中的第一个UIScrollView,(我们正在扫描它,所以这不应该在以后的iOS版本中破坏)。
将UIWebViews框架设置为scrollViews大小。
并将scrollView的contentSize设置为webViews contentSize(或者现在,它的框架)。

After the weView has finished loading. Scan for the first UIScrollView in the webView, (we are scanning for it so this shouldn't break on later iOS versions). Set the UIWebViews frame to it's scrollViews size. And set the scrollView's contentSize to the webViews contentSize (or by now, it's frame).

-(void)setup{
    //Get the webView's first scrollView (the one all the content is in).
    UIScrollView *webViewContentView;
    for (UIView *checkView in [webView subviews] ) {
        if ([checkView isKindOfClass:[UIScrollView class]]) {
            webViewContentView = (UIScrollView*)checkView;
            break;
        }
    }

    webView.frame = CGRectMake(0, 0, webViewContentView.contentSize.width, webViewContentView.contentSize.height);
    scrollView.contentSize = webViewContentView.contentSize;
    scrollView.delegate = self;
}

要启用缩放,您需要从scrollViews委托中添加额外的方法。

To enable zooming you'll need to add an extra method from the scrollViews delegate.

-(UIView *) viewForZoomingInScrollView:(UIScrollView *)scrollView {
    return webView;
}

您可以下载示例项目 http://www.multiupload.com/P8SOZ4NW6C (这是一个xcode 4项目)。

And you can download a sample project http://www.multiupload.com/P8SOZ4NW6C (It's an xcode 4 project).

注意:如果你放大到很多因为webView不知道要重新渲染东西,并且因为你无法点击链接(但是在PDF中),缩放会对webView的内容进行像素化处理这应该不是问题。)

NOTES: The zooming pixelates the webView's content if you zoom in to much because the webView doesn't know to re-render things, and of cause you can't click links (but in a PDF that shouldn't be a problem).

编辑2:更好的方法

自写这篇文章以来,我已经实现了一个更简单,更简单的方法,这也应该解决像素化问题。

Since writing this I have realised a MUCH simpler and easier method, which should also solve the pixelation problem.

-(void)removeInput{
    UIScrollView *webViewContentView;
    for (UIView *checkView in [webView subviews] ) {
    if ([checkView isKindOfClass:[UIScrollView class]]) {
        webViewContentView = (UIScrollView*)checkView;
            break;
        }
    }

    for (UIView *checkView in [webViewContentView subviews] ) {
        checkView.userInteractionEnabled = NO;
    }
}

现在你应该只能与UIScrollView进行交互了本身,意思是缩放,滚动,等等......应该仍然可以工作(当你缩放时页面应该重新渲染而不是像素化),但是你不能选择任何文字,或输入任何内容。

Now you should only be able to interact with the UIScrollView itself, meaning zooming, scrolling, ect.. should still work (and the page should re-render rather the pixelating when you zoom) but you can't select any text, or type anything in.

此方法也基于这个UIWebView子视图列表 可能可以在页面开始加载之前设置它,而不是在它加载页面之后设置它。

Also something about this method is that based on this list of UIWebView subViews it might be possible to set this before the page starts loading rather then after it has finished loading the page.

这篇关于显示文档时没有复制/粘贴和选择矩形的UIWebView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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