4

UIWebView を埋め込む IPhone アプリケーションを作成しています。ナビゲーションなどのさまざまなサファリのような機能があります。私が探しているタスクの 1 つは、ユーザーが Web ビューでテキストを選択したときに「すべて選択」オプションを表示することです。現在、「コピー」オプションしか表示されません。「すべて選択」メニュー項目を有効にする簡単な方法はありますか? もちろん、共有メニューコントローラーにメニュー項目を追加しようとしましたが、必ずしも元のサファリの「すべて選択」機能を実装するとは限りません。ヘルプとポインタは非常に役立ちます。

前もって感謝します。

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 の既定の動作ではなく、[すべて選択] を押してもアプリはクラッシュしませんが、押しても何も起こりません。

.select()javascript メソッドは Mobile Safari/UIWebView では機能しないため、selectAll メソッドを作成して webview ですべてを選択することさえできません。

于 2011-02-12T17:21:52.213 に答える
0

次のselectAllカテゴリの.webViewUIWebView

主なアイデアはUIWebBrowserView、サブビューであることは、パブリックプロトコルと同等のプロトコルに準拠するUIWebViewサブクラスであるというヒントを使用することですUIWebDocumentViewUITextInputPrivateUITextInput

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

標準的な方法ですが、プロジェクト内のすべての webView に不可逆的に影響します。

于 2015-03-29T22:39:20.590 に答える