如何在iPhone应用程序的UIWebView中启用“全选”?

时间:2011-01-22 08:54:19

标签: ios4 uiwebview iphone uiwebviewdelegate

我正在编写一个嵌入UIWebView的iPhone应用程序。有各种类似于Safari的功能,例如导航等。我正在寻找的任务之一是当用户选择Web视图上的一段文本时呈现“全选”选项。目前,我只看到“复制”选项。有没有简单的方法来启用“全选”菜单项?我当然尝试将一个菜单项添加到共享菜单控制器,但这并不一定实现原始的safari“全选”功能。任何帮助和指针都非常有用。

提前致谢。

2 个答案:

答案 0 :(得分:2)

简短的回答是否定的,这是不可能的。

您可以通过继承UIWebView并覆盖(不自行添加任何菜单到菜单控制器)来完成此操作:

-(BOOL) canPerformAction:(SEL)action withSender:(id)sender 

检查选择器是否为selectAll:

像这样:

-(BOOL) canPerformAction:(SEL)action withSender:(id)sender {
    if (action == @selector(selectAll:)) {
        return YES;
    } else {
        return [super canPerformAction:action withSender:sender];
    }
}

这将显示“保持”菜单上的“全选”选项。但是,这不是webView的默认行为,而当您按下全选时应用程序不会崩溃,按下它将不执行任何操作。

你甚至无法创建一个selectAll方法,然后在webview中选择所有内容,因为javascript方法.select()在Mobile Safari / UIWebView上不起作用。

答案 1 :(得分:0)

您可以在运行时使用selectAll的以下类别为不可编辑的webView(相当于Apple的Mail.app上的行为)实施UIWebView行为。

主要思想是使用UIWebBrowserView作为UIWebView的子视图的提示是UIWebDocumentView的子类,它符合UITextInputPrivate协议,它等同于public UITextInput {1}}协议

//  UIWebView+SelectAll.h
//  Created by Alexey Matveev on 28.03.15.
//  Copyright (c) 2015 Alexey Matveev. All rights reserved.

@interface UIWebView (SelectAll)
+ (void)setEnableSelectAll:(BOOL)enabled;
@end


#import "UIWebView+SelectAll.h"
#import <objc/runtime.h>

/*
 UIWebDocumentView is the superclass for UIWebBrowserView.
 UIWebDocumentView conforms UITextInputPrivate protocol which is identival to UITextInput
*/

static IMP canPerformActionWithSenderImp;

@implementation UIWebView (SelectAll)

@dynamic enableSelectAll;

- (BOOL)customCanPerformAction:(SEL)action withSender:(id)sender
{
    if (action == @selector(selectAll:)) {
        return ! self.isSelectedAll;
    }
    else {
        BOOL(*imp)(id, SEL, SEL, id) = (BOOL(*)(id, SEL, SEL, id))canPerformActionWithSenderImp;
        return imp(self, @selector(canPerformAction:withSender:),  action, sender);
    }
}

- (void)selectAll:(id)sender
{
    [self.browserView selectAll:sender];
}

- (UIView<UITextInput> *)browserView
{
    UIView *browserView;
    for (UIView *subview in self.scrollView.subviews) {
        if ([subview isKindOfClass:NSClassFromString(@"UIWebBrowserView")]) {
            browserView = subview;
            break;
        }
    }

    return (UIView<UITextInput> *)browserView;
}

- (BOOL)isSelectedAll
{
    UITextRange *currentRange = self.browserView.selectedTextRange;
    if ([self.browserView comparePosition:currentRange.start toPosition:self.browserView.beginningOfDocument] == NSOrderedSame) {
        if ([self.browserView comparePosition:currentRange.end toPosition:self.browserView.endOfDocument] == NSOrderedSame) {
            return YES;
        }
    }
    return NO;
}

+ (void)setEnableSelectAll:(BOOL)enabled
{
    SEL canPerformActionSelector = @selector(canPerformAction:withSender:);

    if (!canPerformActionWithSenderImp) {
        canPerformActionWithSenderImp = [self instanceMethodForSelector:canPerformActionSelector];
    }

    IMP newCanPerformActionWithSenderImp = enabled ? [self instanceMethodForSelector:@selector(customCanPerformAction:withSender:)] : canPerformActionWithSenderImp;

    Method canPerformActionMethod = class_getInstanceMethod([self class], canPerformActionSelector);
    class_replaceMethod([self class], canPerformActionSelector, newCanPerformActionWithSenderImp, method_getTypeEncoding(canPerformActionMethod));
}

@end

当然,您可以使用全局方法调配

- (BOOL)canPerformAction:(SEL)action withSender:(id)sender;

以标准方式,但它会不可逆转地影响项目中的所有webView。