Could anyone help me to disable the Copy / Paste pup-up when using a testfield ?
Thanks
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)
}
}
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) }
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
}
}
}