Show iPhone cut copy paste menu on UILabel

前端 未结 12 1256
半阙折子戏
半阙折子戏 2020-11-30 21:05
  1. Can we enable the cut copy paste menu for a UILabel as it is for a UITextField?

  2. If not, and I need to convert my UIL

12条回答
  •  Happy的楠姐
    2020-11-30 21:11

    Swift 5.3 and SwiftUI

    To make this work in SwiftUI we can use the method that pableiros created an combine that with a UIViewRepresentable.

    There are two updates that we need to make to the CopyableLabel class as the following methods were deprecated in iOS 13.

    .setTargetRect(_,in:)

    .setMenutVisible(_,animated)

    We can easily fix this by using the .showMenu(from:rect:) method instead.

    Here is the updated CopyableLabel class.

    class CopyableLabel: UILabel {
    
        override init(frame: CGRect) {
            super.init(frame: frame)
            self.sharedInit()
        }
    
        required init?(coder aDecoder: NSCoder) {
            super.init(coder: aDecoder)
            self.sharedInit()
        }
    
        func sharedInit() {
            self.isUserInteractionEnabled = true
            self.addGestureRecognizer(UILongPressGestureRecognizer(target: self, action: #selector(self.showMenu)))
        }
    
        @objc func showMenu(sender: AnyObject?) {
            self.becomeFirstResponder()
    
            let menu = UIMenuController.shared
    
            if !menu.isMenuVisible {
                menu.showMenu(from: self, rect: self.bounds) // <-  we update the deprecated methods here
            }
        }
    
        override func copy(_ sender: Any?) {
            let board = UIPasteboard.general
    
            board.string = text
    
            let menu = UIMenuController.shared
    
            menu.showMenu(from: self, rect: self.bounds) // <- we update the deprecated methods here
        }
    
        override var canBecomeFirstResponder: Bool {
            return true
        }
    
        override func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool {
            return action == #selector(UIResponderStandardEditActions.copy)
        }
    }
    

    Then to get this class to work with SwiftUI all we have to do is create a simple UIViewRepresentable.

    struct CopyableLabelView: UIViewRepresentable {
    
        let text: String
        private let label = CopyableLabel(frame: .zero)
    
        init(text: String) {
            self.text = text
        }
    
        func makeUIView(context: Context) -> UILabel {
            // Set the text for the label
            label.text = text
    
            // Set the content hugging priority so the UILabel's view is
            // kept tight to the text.
            label.setContentHuggingPriority(.required, for: .horizontal)
            label.setContentHuggingPriority(.required, for: .vertical)
            return label
        }
    
        func updateUIView(_ uiView: UILabel, context: Context) {
            // Handle when the text that is passed changes
            uiView.text = text
        }
    }
      
    

提交回复
热议问题