• 给文本标签UILabel添加长按复制功能


    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)
        }
    }
  • 相关阅读:
    UVA11039
    UVA10970大块巧克力
    UVA10970大块巧克力
    UVA10340子序列
    UVA10340子序列
    UVA10382喷水装置
    UVA10382喷水装置
    UVA10020(最小区间覆盖)
    洛谷 P2141 珠心算测验
    UVa 11292 勇者斗恶龙(The Dragon of Loowater)
  • 原文地址:https://www.cnblogs.com/argenbarbie/p/5795271.html
Copyright © 2020-2023  润新知