Home > AI > Uncategorized

UIMenuController复制

直接上码

>>>MenuLabel<<<<<支持复制的label

import Foundation
import UIKit

class MenuLabel: UILabel {
    convenience init(){
        self.init(frame: .zero)
        isUserInteractionEnabled = true
        let lp = UILongPressGestureRecognizer(target: self, action: #selector(self.toggleMenu) )
        self.addGestureRecognizer(lp)
    }
    

    @objc func toggleMenu(){
        
        becomeFirstResponder()
        
        let m = UIMenuController.shared
        let select = UIMenuItem(title: "Select", action: #selector(self.menuSelect))
        let selectAll = UIMenuItem(title: "SelectAll", action: #selector(self.menuSelectAll))
        let cut = UIMenuItem(title: "Cut", action: #selector(self.menuCut))
        let paste = UIMenuItem(title: "Paste", action: #selector(self.menuPaste))
        let delete = UIMenuItem(title: "Delete", action: #selector(self.menuDelete))
        m.menuItems = [select, selectAll, cut, paste, delete]

        
        if !m.isMenuVisible {
            m.setTargetRect(self.bounds, in: self)
            m.setMenuVisible(true, animated: true)
        }
 
    
       
    }
    
    //配合becomeFirstResponder
    override var canBecomeFirstResponder: Bool {
        return true
    }
    
    
    //菜单可执行动作
    override func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool {
        
        let actions = [#selector(self.menuDelete), #selector(UIResponderStandardEditActions.copy(_:)), #selector(self.menuCut), #selector(self.menuDelete), #selector(self.menuPaste), #selector(self.menuSelect), #selector(self.menuSelectAll)]
        if actions.contains(action) {
            return true
        } else {
            return false
        }
    }
    
    //复制 - 2
    override func copy(_ sender: Any?) {
        let p = UIPasteboard.general
        p.string = self.text
        
    }
    
    @objc func menuSelect(){
        print("Select")
    }
    @objc func menuSelectAll(){
        print("SelectAll")
    }
    @objc func menuDelete(){
        print("Delete")
    }
    @objc func menuCut(){
        print("Cut")
    }
    @objc func menuPaste(){
        print("Paste")
    }
}

>>>>ViewController<<<<<

    override func viewDidLoad() {
        super.viewDidLoad()

        
    
        //激活菜单
        let a = MenuLabel()
        a.frame = CGRect(x: 100, y: 100, width: 100, height: 20)
        a.text = "操作"
        self.view.addSubview(a)
        a.font = UIFont.boldSystemFont(ofSize: 18)
        a.textColor = #colorLiteral(red: 0.521568656, green: 0.1098039225, blue: 0.05098039284, alpha: 1)
        a.backgroundColor = #colorLiteral(red: 0.4666666687, green: 0.7647058964, blue: 0.2666666806, alpha: 1)
        a.tintColor = #colorLiteral(red: 0.9764705896, green: 0.850980401, blue: 0.5490196347, alpha: 1)
        a.shadowColor = #colorLiteral(red: 0.2392156869, green: 0.6745098233, blue: 0.9686274529, alpha: 1)
        a.textAlignment = .center
        
        a.translatesAutoresizingMaskIntoConstraints = false
        NSLayoutConstraint.activate([
            a.centerXAnchor.constraint(equalTo: self.view.centerXAnchor),
            a.centerYAnchor.constraint(equalTo: self.view.centerYAnchor),
            a.widthAnchor.constraint(equalToConstant: a.frame.width),
            a.heightAnchor.constraint(equalToConstant: a.frame.height)
        ])
        
        
        //显示复制文字
        let show = UILabel(frame: .zero)
        self.view.addSubview(show)
        show.frame = CGRect(x: 100, y: 100, width: 100, height: 100)
        show.text = UIPasteboard.general.string
        
        
    }

 

Related posts:

Leave a Reply