2

I am aware of the solutions listed here:

How to disable copy paste option from UITextField programmatically

and here:

UITextField how to disable the paste?

but that isn't exactly what I want to do. I feel it is a little messy because the user can still tap and hold the text field and see the paste option is available. If the user taps it, the textfield just won't resound to that action.

I want to actually remove the "Paste" option from the list of options that comes up. Is that possible? Any direction would be greatly appreciated. Thanks in advance.

Community
  • 1
  • 1
James
  • 323
  • 3
  • 8
  • Doesn't the second solution work?.. My understanding is that the second solution lets you hide the menu items that you wish to hide, or even hide the menu altogether. – Sergey Kalinichenko Aug 27 '13 at 19:15
  • The answer to the first link you posted prevents the Paste option from ever appearing in the text field's menu. Did you try it? – rmaddy Aug 27 '13 at 20:35
  • @dasblinkenlight, The second solution doesn't work for what I want. I tried it, but it didn't hide the menu at all. I believe that it is supposed to hide the menu altogether, but it doesn't for me..That might be because I'm working in a beta version I'll have to keep an eye on that option. – James Aug 29 '13 at 16:13
  • @rmaddy, I did try the first option, but that, too, didn't work. It doesn't hide the option to paste, but instead only disables a response to the paste event. The user is still able to see the paste option, which is what I want disable. – James Aug 29 '13 at 16:37
  • Use the `canPeformAction:withEvent:` solution in my own app and it properly prevents the Paste menu from appearing at all. You must have something wrong. I suggest you ask a new question about why your specific solution isn't working as expected. Include the code you are using. – rmaddy Aug 29 '13 at 16:41
  • @rmaddy, Here is the code I'm using. ` - (BOOL)canPerformAction:(SEL)action withSender:(id)sender { if ([self.eventDate isEditing] || [self.eventTime isEditing] || [self.eventCategory isEditing]) { if (action == @selector(paste:)) return NO; } return [super canPerformAction:action withSender:sender]; } ` I don't think that there is a canPerformAction: **withEvent:** . I just check if a text field is editing and disable the paste selector appropriately. However, the paste event is never triggered. Not sure why. – James Sep 03 '13 at 23:39
  • That was a typo on my part. I do use `canPerformSelector:withSender:`. – rmaddy Sep 03 '13 at 23:46

3 Answers3

2
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{

    if(textField == txtCardNumber)
    {
        if([string length]>1){
            //Disable to paste action
            return NO;

        }
     }
}
hiwordls
  • 781
  • 7
  • 17
  • 35
1

Another way is subclassing UITextField and overriding canPerformAction:withSender: method like described in Adding a dynamic custom UIMenuItem to Copy & Paste Menu before it shows

@implementation MyTextView
- (BOOL)canPerformAction:(SEL)action withSender:(id)sender {
    if (action == @selector(paste:)) {
        return NO;
    }
    return [super canPerformAction:action withSender:sender];
}
@end
Community
  • 1
  • 1
Serhiy
  • 332
  • 6
  • 13
0

Use below Method

-(BOOL)canPerformAction:(SEL)action withSender:(id)sender
{
     //Do your stuff
     [[NSOperationQueue mainQueue] addOperationWithBlock:^{
     [[UIMenuController sharedMenuController] setMenuVisible:NO animated:NO];
     }];
     return [super canPerformAction:action withSender:sender];
}
Hardik Mamtora
  • 1,642
  • 17
  • 23