自定义view添加复制粘贴功能

在有很多信息需要复制,但是布局又是采用的自定义view的时候,可以给自定义的view添加复制功能,自定义需要复制的内容

一、iOS已经有一些控件自带了复制功能
1、UITextView
2、UITextField
3、UIWebView
4、WKWebView

一些简单的文本如果需要复制,可以直接使用UITextView替代UILabel,但是如果想UILabe或者自定义的view也有复制的功能,可以继续往下看

二、需要知道的内容
1、UIPasteboard -- 用来放入需要复制的内容,可以放文本和图片,url等
2、UIMenuController -- 用来创建显示的菜单选项

自定义view添加复制粘贴功能_第1张图片
自定义的菜单选项

3、自定义view实现canPerformAction:withSender:方法,他返回一个bool值,用来判断哪些方法对应的菜单可以显示出来

    override func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool {
        if action == #selector(self.customAction) {
            return true
        }
        if action == #selector(self.moreMenuAction) {
            return true
        }
        
        return false
    }

实现canBecomeFirstResponder属性,代表改自定义view可以成为第一响应者,这样才能获得相应事件

override var canBecomeFirstResponder: Bool {
        return true
    }

自定义改view的时候,还需要设置属性isUserInteractionEnabled为true
self.isUserInteractionEnabled = true

三、注意上面的内容,就可以给自定义的view加上复制粘贴以及更多的高级功能了

class MyCopyView: USBaseView {
    let label1 = UILabel()
    let textField = UITextField()
    
    override func setup() {
        super.setup()
        
        self.isUserInteractionEnabled = true
        
        self.label1.us.customize { (view) in
            self.addSubview(view)
            
            view.isUserInteractionEnabled = true
            view.text = "复制的数据"
            view.textColor = UIColor.red
        }
        
        self.textField.us.customize { (view) in
            self.addSubview(view)
            
            view.backgroundColor = UIColor.red
            view.text = "填写数据"
            view.textColor = UIColor.green
        }
        
        
        self.label1.snp.makeConstraints { (make) in
            make.top.leading.bottom.equalToSuperview()
            make.width.equalTo(Utils.screenWidth / 2)
        }
        
        self.textField.snp.makeConstraints { (make) in
            make.leading.equalTo(self.label1.snp.trailing)
            make.top.trailing.equalToSuperview()
            make.height.equalTo(30)
        }
        
        let gesture = UILongPressGestureRecognizer(target: self, action: #selector(self.handleLongGesture(recognizer:)))
        self.addGestureRecognizer(gesture)
        
        
    }
    
    override var canBecomeFirstResponder: Bool {
        return true
    }
    
    override func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool {
        if action == #selector(self.customAction) {
            return true
        }
        if action == #selector(self.moreMenuAction) {
            return true
        }
        
        return false
    }
    
    func handleLongGesture(recognizer: UIGestureRecognizer) {
        if let recognizerView = recognizer.view,
            let recognizerSuperView = recognizerView.superview
        {
            if !UIMenuController.shared.isMenuVisible {
                let menuController = UIMenuController.shared
                let item1 = UIMenuItem(title: "自定义功能1", action: #selector(self.customAction))
                let item2 = UIMenuItem(title: "...", action: #selector(self.moreMenuAction))
                menuController.menuItems = [item1, item2]
                menuController.setTargetRect(recognizerView.frame, in: recognizerSuperView)
                menuController.setMenuVisible(true, animated:true)
                recognizerView.becomeFirstResponder()
            }
        }
    }
    
    func customAction() {
        print("自定义功能1,这个就叫做复制吧")
        
        UIPasteboard.general.string = self.label1.text
//        UIPasteboard.general.image = UIImage(named: "icon_touch") // 复制图片
    }
    
    func moreMenuAction() {
        // 更多功能可以展示弹出框
    }
 
}

你可能感兴趣的:(自定义view添加复制粘贴功能)