使用 UIEditMenu 与 UITextView 交互

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

我们如何使用 UIEditMenuInteraction 与 UITextView 来自定义菜单并添加更多按钮?

在 iOS 16 之前我使用的是:

UIMenuController.shared.menuItems = [menuItem1, menuItem2, menuItem3]
swift uitextview ios16
2个回答
6
投票

尝试这个示例源:

class ViewController: UIViewController {
    
    @IBOutlet weak var txtView: UITextView!
    
    var editMenuInteraction: UIEditMenuInteraction?
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        setupEditMenuInteraction()
    }
    
    private func setupEditMenuInteraction() {
        
        // Addding Menu Interaction to TextView
        editMenuInteraction = UIEditMenuInteraction(delegate: self)
        txtView.addInteraction(editMenuInteraction!)
        
        // Addding Long Press Gesture
        let longPressGestureRecognizer =
        UILongPressGestureRecognizer(target: self, action: #selector(handleLongPress(_:)))
        txtView.addGestureRecognizer(longPressGestureRecognizer)
    }
    
    @objc
    func handleLongPress(_ gestureRecognizer: UILongPressGestureRecognizer) {
        guard gestureRecognizer.state == .began else { return }
        
        let configuration = UIEditMenuConfiguration(
            identifier: "textViewEdit",
            sourcePoint: gestureRecognizer.location(in: txtView)
        )
        
        editMenuInteraction?.presentEditMenu(with: configuration)
    }
}
extension ViewController: UIEditMenuInteractionDelegate {
    func editMenuInteraction(_ interaction: UIEditMenuInteraction,
                             menuFor configuration: UIEditMenuConfiguration,
                             suggestedActions: [UIMenuElement]) -> UIMenu? {
        
        var actions = suggestedActions
        
        let customMenu = UIMenu(title: "", options: .displayInline, children: [
            UIAction(title: "menuItem1") { _ in
                print("menuItem1")
            },
            UIAction(title: "menuItem2") { _ in
                print("menuItem2")
            },
            UIAction(title: "menuItem3") { _ in
                print("menuItem3")
            }
        ])
        
        actions.append(customMenu)
        
        return UIMenu(children: actions) // For Custom and Suggested Menu
        
        return UIMenu(children: customMenu.children) // For Custom Menu Only
    }
}

输出


0
投票

文档中的

editMenu
解决方案使用标准印刷机,并对突出显示的文本执行功能。

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.