I am using the SkyFloatingLabelTextField class for UITextfield,How can I disable the Copy and paste functionality on this textfiled.
Asked
Active
Viewed 326 times
0
-
1You can do that by overriding the "canPerformAction" method. Check this answer. https://stackoverflow.com/questions/29596043/how-to-disable-pasting-in-a-textfield-in-swift – Umair Ahmed Oct 07 '21 at 06:55
2 Answers
2
Use this technique for custom textField
class SkyFloatingLabelTextField: UITextField {
open override func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool {
if action == #selector(UIResponderStandardEditActions.paste(_:)) {
return false
}
return super.canPerformAction(action, withSender: sender)
}
}

Jayesh Patel
- 938
- 5
- 16
-
Already assigned a class SkyfloatingLabelTextField to the UITextField @Jayesh Patel – Manish Kumar Oct 07 '21 at 06:55
-
1Then add it in your SkyFloatingLabelTextField class. i have updated the answer – Jayesh Patel Oct 07 '21 at 07:02
1
Create a custom class inherited from SkyFloatingLabelTextField class and then assign.
class FloatingTextField: SkyFloatingLabelTextField {
open override func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool {
if action == #selector(UIResponderStandardEditActions.paste(_:)) ||
action == #selector(UIResponderStandardEditActions.copy(_:)) {
return false
}
return super.canPerformAction(action, withSender: sender)
}
}
If you want for the whole project and all textfield add this extension.
extension SkyFloatingLabelTextField {
open override func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool {
if action == #selector(UIResponderStandardEditActions.paste(_:)) ||
action == #selector(UIResponderStandardEditActions.copy(_:)) {
return false
}
return super.canPerformAction(action, withSender: sender)
}
}

Raja Kishan
- 16,767
- 2
- 26
- 52