how to disable copy/paste in textfield ?

Could anyone help me to disable the Copy / Paste pup-up when using a testfield ?


Thanks

You have to use a custom UITextField and set your UITextField component to use a custom class:


class TextField: UITextField {
    override func canPerformAction(action: Selector, withSender sender: AnyObject?) -> Bool {
        if action == Selector("paste:") || action == Selector("copy:") {
            return false
        }
       
        return super.canPerformAction(action, withSender: sender)
    }
}
In swift 5 the code looks like this:
Code Block
    open override func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool {
        if action == #selector(UIResponderStandardEditActions.paste(_:)) {
            return false
        }
        return super.canPerformAction(action, withSender: sender)
    }

You still have to use a custom UITextField and set your UITextField component to use a custom class (as mentioned before).


   

If you want to disable pasting for a specific TextField you can use UITextFieldDelegate. Below code is working to find a specific TextField and stop pasting.

class TextClass: UITextFieldDelegate { 
	func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
		if textField == yourSpecificTextFiled{
			if UIPasteboard.general.isKind(of: UIPasteboard.self){
				return false
			}
			return true
		}
	}
}
how to disable copy/paste in textfield ?
 
 
Q