iOS UITextView UIMenuController 去除自带的item 2022-04-18


import UIKit
import PDFKit
import CoreGraphics

class ViewController: UIViewController, UITextViewDelegate {
    override func viewDidLoad() {
        super.viewDidLoad()
        let textview = nowTextView()
        textview.frame = CGRect.init(x: 0, y: 300, width: 200, height: 100);
        self.view.addSubview(textview)
        textview.text = "这是测试"
        textview.textColor = .black
        textview.delegate = self
        textview.isEditable = false
        textview.tintColor = .clear

        let menuController = UIMenuController.shared
        let item1 = UIMenuItem(title: "测试1", action: #selector(ViewController.test))
        let item2 = UIMenuItem(title: "测试2", action: #selector(ViewController.test))
        menuController.menuItems = [item1, item2]

        
    }
  
   @objc func test() {
        print("123456")
    }

}
class nowTextView:UITextView {
    
    open override func canPerformAction(_ action: Selector, withSender
        sender: Any?) -> Bool {
//打印事件  想显示的 返回true 
         print(action)

        if action == #selector(cut(_:)) {
            return true
        }
        return false
    }
    
}

效果图

image.png

你可能感兴趣的:(iOS UITextView UIMenuController 去除自带的item 2022-04-18)