4

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

提前致谢。

4

2 回答 2

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 上不起作用。

于 2011-02-12T17:21:52.213 回答
0

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

主要思想是使用暗示,UIWebBrowserView作为的子视图UIWebViewUIWebDocumentView符合UITextInputPrivate协议的子类,相当于公共UITextInput协议

//  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

当然,你可以使用全局方法 swizzling

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

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

于 2015-03-29T22:39:20.590 回答