2011-01-22 82 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。

相關問題