2011-01-22 93 views
4

我正在写一个嵌入了UIWebView的iPhone应用程序。有像导航等功能的各种野生动物园。我正在寻找的任务之一是当用户选择Web视图上的文本时提供“全选”选项。目前,我只看到一个“复制”选项。有没有简单的方法来启用“全选”菜单项?我曾经尝试向共享的菜单控制器添加菜单项,但不一定实现原始Safari浏览器的“全选”功能。任何帮助和指针都会非常有用。如何在iPhone应用程序的UIWebView中启用“全选”?

在此先感谢。

回答

2

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

你可以通过继承的UIWebView并重写做到这一点(不添加anythings到菜单控制自己):

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

另外应该注意的是,在文档中不允许子类UIWebView。 – BadPirate 2011-05-13 20:26:49

0

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

主要思想是使用暗示UIWebBrowserView是的UIWebView子视图是UIWebDocumentView这符合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 

当然,你也可以使用全局方法调整为

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

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

相关问题