Ios4 如何启用";选择全部";在IPhone应用程序的UIWebView中?
我正在编写一个嵌入UIWebView的IPhone应用程序。有各种类似safari的功能,如导航等。我正在寻找的任务之一是,当用户在web视图上选择一段文本时,提供一个“全选”选项。目前,我只看到“复制”选项。是否有一种简单的方法来启用“全选”菜单项?我当然尝试过向共享菜单控制器添加菜单项,但这并不一定实现原始的safari“全选”功能。任何帮助和指点都将非常有用Ios4 如何启用";选择全部";在IPhone应用程序的UIWebView中?,ios4,uiwebview,iphone,uiwebviewdelegate,Ios4,Uiwebview,Iphone,Uiwebviewdelegate,我正在编写一个嵌入UIWebView的IPhone应用程序。有各种类似safari的功能,如导航等。我正在寻找的任务之一是,当用户在web视图上选择一段文本时,提供一个“全选”选项。目前,我只看到“复制”选项。是否有一种简单的方法来启用“全选”菜单项?我当然尝试过向共享菜单控制器添加菜单项,但这并不一定实现原始的safari“全选”功能。任何帮助和指点都将非常有用 提前谢谢。简短的回答是不,这是不可能的 可以通过将UIWebView子类化并重写(无需自己向菜单控制器添加任何内容)来实现这一点:
提前谢谢。简短的回答是不,这是不可能的 可以通过将UIWebView子类化并重写(无需自己向菜单控制器添加任何内容)来实现这一点: 并检查选择器是否为
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上不起作用。你可以为不可编辑的webview
实现selectAll
行为(相当于苹果Mail.app上的行为)在运行时为UIWebView
使用以下类别
主要思想是使用UIWebBrowserView
作为UIWebView
的子视图的提示是UIWebDocumentView
的子类,该子类符合uideputPrivate
协议,该协议相当于publicuideput
协议
// 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
以标准方式,但它将不可逆地影响项目中的所有Web视图。还应注意,文档中不允许对UIWebView进行子类化。
// 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;