http://www.111cn.net/sj/iOS/104236.htm

http://blog.csdn.net/lrenjun/article/details/12582927

 

自定义一个可复制的标签类,使其能够响应Touch事件并显示复制菜单。

class CopyLabel: UILabel {
    override init(frame: CGRect) {
        super.init(frame: frame)
        userInteractionEnabled = true
        let longPress = UILongPressGestureRecognizer(target: self,
                                                     action: #selector(handleLongPress(_:)))
        addGestureRecognizer(longPress)
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func canBecomeFocused() -> Bool {
        return true
    }
    
    override func canPerformAction(action: Selector, withSender sender: AnyObject?) -> Bool {
        return action == #selector(NSObject.copy(_:))
    }
    
    override func copy(sender: AnyObject?) {
        let pBoard = UIPasteboard.generalPasteboard()
        pBoard.string = self.text
    }
    
    func handleLongPress(gesture: UILongPressGestureRecognizer) {
        self.becomeFirstResponder()
        let menu = UIMenuController.sharedMenuController()
        menu.setTargetRect(self.frame, inView: self)
        menu.setMenuVisible(true, animated: true)
    }
}

 

相关文章:

  • 2022-12-23
  • 2021-11-24
  • 2022-12-23
  • 2021-08-25
  • 2021-06-26
  • 2021-06-27
  • 2021-06-12
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2021-09-02
  • 2022-12-23
  • 2021-04-07
  • 2022-03-15
  • 2022-12-23
相关资源
相似解决方案