2011-01-22 23 views
4

私はUIWebViewを埋め込んだIPhoneアプリケーションを書いています。ナビゲーションなどのようなさまざまなサファリがあります。私が探しているタスクの1つは、ユーザーがウェブビュー上のテキストを選択すると「すべて選択」オプションを提示することです。現在、私は「コピー」オプションしか見ていません。 「すべて選択」メニュー項目を有効にする簡単な方法はありますか?私はofcourseにメニュー項目を共有メニューコントローラに追加しようとしましたが、必ずしも元のサファリの「すべて選択」機能を実装するとは限りません。どんな助けとポインタも非常に便利です。iPhoneアプリケーションのUIWebViewで「すべて選択」を有効にするにはどうすればよいですか?

ありがとうございます。

答えて

2

短い答えは、これはできません。

-(BOOL) canPerformAction:(SEL)action withSender:(id)sender { 
    if (action == @selector(selectAll:)) { 
     return YES; 
    } else { 
     return [super canPerformAction:action withSender:sender]; 
    } 
} 
:セレクタがこのよう selectAll:

ある場合

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

そしてチェック:

あなたがのUIWebViewをサブクラス化し、オーバーライドすることによってこれを行うことができます(メニューコントローラ自分にanythingsを追加せずに)

これは、[保留]メニューの[すべて選択]オプションを表示します。しかし、これはWebViewのデフォルトの動作ではなく、Select Allを押してもアプリケーションがクラッシュすることはありませんが、押しても何も起こりません。

javascriptのメソッド.select()がモバイルSafari/UIWebViewでは機能しないため、selectAllメソッドを作成してからwebviewのすべてを選択することもできません。

+1

また、UIWebViewのサブクラス化はドキュメントでは禁止されています。 – BadPirate

0

編集可能でないwebViewの動作(AppleのMail.appでの動作に相当)を、実行時にUIWebViewのカテゴリを使用して実装できます。

主なアイデアは、あなたがグローバルに使用できるUIWebBrowserViewはもちろん

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

公共UITextInputプロトコルに相当しUITextInputPrivateプロトコルに準拠しUIWebDocumentViewのサブクラスはUIWebViewのサブビューされているというヒントを使用することですこの方法は、標準的な方法で

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

ためスウィズルが、それはirreversably、プロジェクト内のすべてのwebViewsに影響します。

関連する問題