zoukankan      html  css  js  c++  java
  • 给文本标签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)
        }
    }
  • 相关阅读:
    UOJ 30 【CF Round #278】Tourists
    CodeForces 1070F Katya and Segments Sets
    Django系列3:Model的简单对接
    Django系列2:MTV简单流程
    python virtualenv
    vscode使用
    Django 系列1:Django简介
    vue-cli3/4 vue ui创建elementui项目
    npm安装参数
    vue系列11:vuex
  • 原文地址:https://www.cnblogs.com/argenbarbie/p/5795271.html
Copyright © 2011-2022 走看看