[英]Disable copy, paste in UITextfield is not working in iOS 9.x
我在一個類 BBCustomUtility.h,.m 類文件中創建了一個文本字段,然后
+(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 ;
}
從普通類導入並在 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];
嘗試在以下方法中禁用文本字段上的復制粘貼選項這些對我不起作用
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
任何人都可以告訴我我在代碼中犯了什么錯誤。
在您的代碼中添加以下方法,就是這樣,
- (BOOL)canPerformAction:(SEL)action withSender:(id)sender {
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
[[UIMenuController sharedMenuController] setMenuVisible:NO animated:NO];
}];
return [super canPerformAction:action withSender:sender];
}
它將禁用所有類型的編輯。
希望這會有所幫助:)
如果您只想將其應用於一個特殊的文本字段,例如密碼:
- (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];
}
另一種選擇是直接避免選擇實現 UITextFieldDelegate 的 Textfield 並在其方法中進行設置:
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
textField.userInteractionEnabled = NO;
return YES;
}
- (void)textFieldDidEndEditing:(UITextField *)textField {
textField.userInteractionEnabled = YES;
}
希望它有幫助。
-canPerformAction:withSender: 應該在 UITextField 的子類中。 它看起來不像你子類化,因為你正在分配一個 UITextField。
//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];
}
斯威夫特 5
override func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool {
OperationQueue.main.addOperation {
UIMenuController.shared.setMenuVisible(false, animated: false)
}
return super.canPerformAction(action, withSender: sender)
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.