Elias Al Zaghrini
Elias Al Zaghrini

Reputation: 295

Make UiTextField accept only paste option - Swift

I want to make a UiTextField to accept only paste option when long pressed, and do not show the keyboard nor the cursor like the one in the iOS phone app.

I tried to remove the keyboard by using the delegate method, but now the paste option does appear.

Can you help me please?

@IBOutlet weak var phoneNumberTf:UITextField? {
didSet {
    phoneNumberTf?.backgroundColor = .clear
    phoneNumberTf?.textAlignment = .left
    phoneNumberTf?.font = UIFont().font_regular(ofSize: 38)
    phoneNumberTf?.delegate = self
    }
}
extension KeypadView: UITextFieldDelegate {
    func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {
        return false
    }
}

Upvotes: 2

Views: 766

Answers (1)

rohanphadte
rohanphadte

Reputation: 1028

You can achieve this by creating your by subclassing UILabel with a long press gesture recognizer and setting UIMenuController.

class MenuLabel: UILabel {

    override var canBecomeFirstResponder: Bool {
        return true
    }

    // MARK: - Init

    override init(frame: CGRect) {
        super.init(frame: frame)
        commonInit()
    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        commonInit()
    }

    private func commonInit() {
        isUserInteractionEnabled = true
        addGestureRecognizer(
            UILongPressGestureRecognizer(
                target: self,
                action: #selector(handleLongPressed(_:))
            )
        )
    }

    // MARK: - Actions

    @objc internal func handleLongPressed(_ gesture: UILongPressGestureRecognizer) {
        guard let gestureView = gesture.view, let superView = gestureView.superview else {
            return
        }

        let menuController = UIMenuController.shared

        guard !menuController.isMenuVisible, gestureView.canBecomeFirstResponder else {
            return
        }

        gestureView.becomeFirstResponder()

        menuController.menuItems = [
            UIMenuItem(
                title: "Paste",
                action: #selector(handlePasteAction(_:))
            ),
        ]

        menuController.showMenu(from: superView, rect: gestureView.frame)
    }

    @objc internal func handlePasteAction(_ controller: UIMenuController) {
        self.text = UIPasteboard.general.string
    }
}

Upon long press, the UIMenuController will present a paste option, which then sets the UILabel's text value to UIPasteboard.general.string.

Upvotes: 2

Related Questions