Custom UITextField to disallow copy, paste, and other actions
suggest changeTo 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.
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents