I tried
@implementation UITextField (DisableCopyPaste)
-(BOOL)canPerformAction:(SEL)action withSender:(id)sender
{
return NO;
return [super canPerformAction:action withSender:sender];
}
@end
But it disables all textfield's copy/paste option,how to disable the menu options for specific textfield.
I think this method is ok,since no making of category etc.It works fine for me.
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
[[UIMenuController sharedMenuController] setMenuVisible:NO animated:NO];
}];
return [super canPerformAction:action withSender:sender];
You should subclass UITextView
and override canPerformAction:withSender
.
Text fields that shouldn't provide copy/paste should be defined with your subclass.
NonCopyPasteField.h:
@interface NonCopyPasteField : UITextField
@end
NonCopyPasteField.m:
@implemetation
(BOOL)canPerformAction:(SEL)action withSender:(id)sender {
if (action == @selector(copy:) || action == @selector(paste:)) {
return NO;
}
[super canPerformAction:action withSender:sender];
}
@end
Update. Swift version:
class NonCopyPasteField: UITextField {
override func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool {
if (action == #selector(copy(_:)) || action == #selector(paste(_:))) {
return false
}
return super.canPerformAction(action, withSender: sender)
}
}
Create a sub class for UITextField and overwrite the method and use it wherever you want.
@interface CustomTextField: UITextField
@end
@implemetation CustomTextField
-(BOOL)canPerformAction:(SEL)action withSender:(id)sender
{
//Do your stuff
}
@end
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With