Disable copy, paste in UITextfield does not work in iOS 9.x

I created a text box in one class of BBCustomUtility.h, .m files, and then

+(UITextField*)createTextField: (CGRect)rect image:(NSString*)imageName tag:(int)tag secureText:(BOOL)entry placeh:(NSString*)placeholder { UITextField *transactionField = [ [ UITextField alloc ] initWithFrame: rect ]; transactionField.background = [UIImage imageNamed:imageName]; transactionField.adjustsFontSizeToFitWidth = YES; transactionField.textColor = [UIColor blackColor]; transactionField.font = [UIFont systemFontOfSize:17.0]; transactionField.placeholder = placeholder; transactionField.backgroundColor = [UIColor clearColor]; transactionField.borderStyle = UITextBorderStyleNone; transactionField.autocorrectionType = UITextAutocorrectionTypeNo; transactionField.autocapitalizationType = UITextAutocapitalizationTypeNone; transactionField.textAlignment = UITextAlignmentCenter; transactionField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter; transactionField.keyboardType = UIKeyboardTypeDefault; transactionField.returnKeyType = UIReturnKeyDone; transactionField.tag = tag; transactionField.delegate = self; transactionField.clearButtonMode = UITextFieldViewModeWhileEditing; transactionField.text = @""; [ transactionField setEnabled: YES ]; transactionField.secureTextEntry = entry; return transactionField ; } 

import from a common class and use in class1.m

 mPasswordField1 = [BBCustomUtility createTextField:CGRectMake(IS_WIDTH_DEVICE/2-120, 140, 50, 50) image:@"txtField_bg_50.png" tag:1 secureText:YES placeh:[shareObj.mLabelDictionary valueForKey:@""]]; mPasswordField1.keyboardType = UIKeyboardTypeNumberPad; mPasswordField1.clearButtonMode = UITextFieldViewModeNever; mPasswordField1.delegate = self; [self.view addSubview:mPasswordField1]; 

tried to disable the copy option in the text box in the methods below that do not work for me

 1) -(BOOL)canPerformAction:(SEL)action withSender:(id)sender { // Returning 'NO' here disables all actions on textfield return NO; } // not working still showing the paste option on textfield 

2)

  -(BOOL)canPerformAction:(SEL)action withSender:(id)sender { if (action == @selector(copy:) || action == @selector(paste:)) { return NO; } return [super canPerformAction:action withSender:sender]; } // this is also not working still showing the paste option 

3)

  -(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string { if ([mPasswordField1 respondsToSelector:@selector(inputAssistantItem)]) { UITextInputAssistantItem *inputAssistantItem = [mPasswordField1 inputAssistantItem]; inputAssistantItem.leadingBarButtonGroups = @[]; inputAssistantItem.trailingBarButtonGroups = @[]; } } // this also not working 

can someone tell me what mistake i made in my code.

+5
source share
4 answers

Add the method below to your code,

  - (BOOL)canPerformAction:(SEL)action withSender:(id)sender { [[NSOperationQueue mainQueue] addOperationWithBlock:^{ [[UIMenuController sharedMenuController] setMenuVisible:NO animated:NO]; }]; return [super canPerformAction:action withSender:sender]; } 

It will disable all types of editing.

Hope this helps :)

+5
source

-canPerformAction: withSender: must be in a subclass of UITextField. This is not like you are subclassing because you are allocating a UITextField.

+1
source

enter image description here

  //This code has side effects in the UISearchbar (white field is no more disappearing): - (BOOL)canPerformAction:(SEL)action withSender:(id)sender { [[NSOperationQueue mainQueue] addOperationWithBlock:^{ [[UIMenuController sharedMenuController] setMenuVisible:NO animated:NO]; }]; return [super canPerformAction:action withSender:sender]; } 
0
source

If you want to apply this to only one special text field, for example, a password:

 - (BOOL)canPerformAction:(SEL)action withSender:(id)sender { if([self.textFieldPassword isFirstResponder]){ [[NSOperationQueue mainQueue] addOperationWithBlock:^{ [[UIMenuController sharedMenuController] setMenuVisible:NO animated:NO]; }]; } return [super canPerformAction:action withSender:sender]; } 

Another option is to avoid selecting a text field that implements UITextFieldDelegate, and do this in its methods:

 - (BOOL)textFieldShouldBeginEditing:(UITextField *)textField { textField.userInteractionEnabled = NO; return YES; } - (void)textFieldDidEndEditing:(UITextField *)textField { textField.userInteractionEnabled = YES; } 

Hope this helps.

0
source

All Articles