2

我有一个显示 pdf 文件的 web 视图。作为网络视图的标准,用户可以选择文本并执行标准操作,如复制、剪切。我需要添加一个自定义 UIMenuController 来显示自定义操作。我不明白如何在标准 UIPasteboard 中复制所选文本并传递给我的自定义选择器。我试过用

[[UIApplication sharedApplication] sendAction:@selector(copy:) to:nil from:self forEvent:nil];

在方法中,我可以复制选定的文本,但我不明白如何将此值返回到我的方法中。

这是我的代码(简化) 在视图中确实加载了我为 UIPasteboardChangedNotification 添加了一个观察者来执行我的选择器 myCopy:

- (void)viewDidLoad
{
[super viewDidLoad];

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(myCopy:) name:UIPasteboardChangedNotification object:nil];

UIMenuItem *schedaMenuItem = [[UIMenuItem alloc] initWithTitle:NSLocalizedString(@"POPSCHEDA",nil) action:@selector(copyScheda:)];
UIMenuItem *istruzionetMenuItem = [[UIMenuItem alloc] initWithTitle:NSLocalizedString(@"POPISTR",nil) action:@selector(copyIstruzione:)];
[[UIMenuController sharedMenuController] setMenuItems:[NSArray arrayWithObjects:MenuItem1,MenuItem2,nil]];

}

-(void)myCopy:(id)sender {
  UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
  NSString* copyCode = pasteboard.string;
  //perform the necessary action
 }

 -(void)copyScheda:(id)sender {
   UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
   NSString* copyCode = pasteboard.string;
   //perform the necessary action
 }

 -(void)copyIstruzione:(id)sender {
   UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
   NSString* copyCode = pasteboard.string;
   //perform the necessary action
 }

感谢您的帮助和时间!

4

1 回答 1

1

如果我正确理解了您的问题,那么您正在尝试获取当前选择的文本,当用户点击您的自定义项目时将其复制到粘贴板,然后对所选/复制的文本执行某些操作?

如果是这样,我认为这应该对你有用,至少它在 Swift 4 中对我有用。

首先,在 中viewDidLoad,设置您的自定义菜单项

let customMenuItem = UIMenuItem(title: "Foo Bar", action: #selector(ViewController.customMenuTapped))
UIMenuController.shared.menuItems = [customMenuItem]
UIMenuController.shared.update()

然后,仍然viewDidLoad将自己设置为观察者UIPasteboardChanged

NotificationCenter.default.addObserver(self, selector: #selector(displaySelectecText), name: NSNotification.Name.UIPasteboardChanged, object: nil)

然后创建响应 的函数customMenuItem,在我的示例中它是customMenuTapped(),并且您基本上将复制操作发送到响应者链来处理它。

@objc func customMenuTapped() {
    UIApplication.shared.sendAction(#selector(copy(_:)), to: nil, from: self, for: nil)
}

由于您已将自己注册为侦听器,因此当复制操作完成后,您的其他函数将被调用,我的是displaySelectecText()

@objc func displaySelectedText() {
    let copiedText = UIPasteboard.general.string ?? ""
    let alertView = UIAlertController(title: "Selected Text", message: copiedText, preferredStyle: .alert)
    alertView.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))
    present(alertView, animated: true, completion: nil)
}

在此示例中,为简单起见,我只是在警报视图中显示文本。

这是完整的代码:

import UIKit
import WebKit

class ViewController: UIViewController {

    @IBOutlet weak var webView: WKWebView!

    override func viewDidLoad() {
        super.viewDidLoad()

        loadWebView()
        setupCustomMenu()
        setupListener()
    }

    func loadWebView() {
        let url = URL(string: "http://www.google.com")
        let request = URLRequest(url: url!)
        webView.load(request)
    }

    func setupCustomMenu() {
        let customMenuItem = UIMenuItem(title: "Foo Bar", action:
            #selector(ViewController.customMenuTapped))
        UIMenuController.shared.menuItems = [customMenuItem]
        UIMenuController.shared.update()
    }

    func setupListener() {
        NotificationCenter.default.addObserver(self, selector: #selector(displaySelectedText), name: NSNotification.Name.UIPasteboardChanged, object: nil)
    }

    @objc func customMenuTapped() {
        UIApplication.shared.sendAction(#selector(copy(_:)), to: nil, from: self, for: nil)
    }

    @objc func displaySelectedText() {
        let copiedText = UIPasteboard.general.string ?? ""
        let alertView = UIAlertController(title: "Selected Text", message: copiedText, preferredStyle: .alert)
        alertView.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))
        present(alertView, animated: true, completion: nil)
    }

}

这是一个演示

在此处输入图像描述

于 2018-04-12T00:41:58.973 回答