Custom UITextField to disallow copy, paste, and other actions

suggest change

To disable Copy, Paste, Replace, Select, and the other editing actions on a UITextField, use a subclass that refuses those actions:

class CustomTextField: UITextField {

    var enableLongPressActions = false

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

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

    override func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool {
        return enableLongPressActions
    }
}

Set enableLongPressActions to true later to allow those actions again. The override applies to every action canPerformAction is asked about, not only the long-press menu.

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents