如何将自定义操作添加到 iOS 中的文本选择编辑菜单?

问题描述 投票:0回答:4

我需要向用户在 iOS 中的 UITextView 中选择某些文本时弹出的编辑菜单添加自定义操作。
我该怎么做?

ios swift menu uitextview edit
4个回答
23
投票
class ViewController: UIViewController, UITextViewDelegate {

   @IBOutlet weak var textView: UITextView!

   override func viewDidLoad() {
      super.viewDidLoad()

      addCustomMenu()
   }

   func addCustomMenu() {
      let printToConsole = UIMenuItem(title: "Print To Console", action: #selector(printToConsole))
      UIMenuController.shared().menuItems = [printToConsole]
   }

   func printToConsole() {
      if let range = textView.selectedTextRange, let selectedText = textView.text(in: range) {
         print(selectedText)
      }
   }
}

这是文本选择菜单项的示例,它将

UITextView
中的文本更改为红色。
changeToRedFunc
可以执行您想要的任何操作。

注意:这是在 Swift 3 中 (询问你是否想要在 Swift 2.3 中使用)

希望这有帮助!如果你有任何问题随时问! :D


4
投票

SWIFT 5

class ViewController: UIViewController, UITextViewDelegate {
    @IBOutlet weak var textView: UITextView!

    override func viewDidLoad() {
        super.viewDidLoad()

         addCustomMenu()
    }


    func addCustomMenu() {
       //Xcode doesn't like printToConsole being a var and a function call
       let printToConsole = UIMenuItem(title: "Print To Console", action: #selector(printToConsole2))

        UIMenuController.shared.menuItems = [printToConsole]
    }

    @objc func printToConsole2() {
       if let range = textView.selectedTextRange, let selectedText = textView.text(in: range) {
          print(selectedText)
       }
    }
}

1
投票

以下是如何在 swift 5 中创建自定义编辑菜单:

import UIKit

class ViewController: UIViewController {

    @IBOutlet var textfield: UIView!
    override func viewDidLoad() {
        super.viewDidLoad()
    
        let changeBackground = UIMenuItem(title: "Change Background Colour", action: #selector(changeBackgroundColour))
        UIMenuController.shared.menuItems = [changeBackground] //will add it to everything that has
    
    }

    @objc func changeBackgroundColour()
    {
        self.view.backgroundColor = .cyan //just makes the background colour cyan
    }

}

我还制作了一个 YouTube 视频来解释这一点这里


0
投票

由于 UIMenuItem 已被弃用,您必须

  1. 使用
    editMenuInteraction
    溶液
  2. 使用
    editMenu
    功能

使用

editMenu
功能非常简单。我还将我的解决方案发布在:stackoverflow 73712955

private class CustomUITextView: UITextView, UIEditMenuInteractionDelegate {
    override func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool {
        // A little short-circuit for our new function to run.
        if action == #selector(printToConsole) {
            return true
        }
        return super.canPerformAction(action, withSender: sender)
    }
    
    override func editMenu(for textRange: UITextRange, suggestedActions: [UIMenuElement]) -> UIMenu? {
        let refreshAction = UIAction(title: "Print to Console") { (action) in
            self.printToConsole()
        }

        var actions = suggestedActions
        actions.insert(refreshAction, at: 0) // insert at the front of the menu.
        return UIMenu(children: actions)
    }
    
    @objc func printToConsole() {
       if let range = self.selectedTextRange, let selectedText = self.text(in: range) {
          print(selectedText)
       }
    }
}

看起来像这样

它可以打印到控制台。

© www.soinside.com 2019 - 2024. All rights reserved.