禁用复制,在UITextfield中粘贴在iOS 9.x中不起作用

时间:2016-06-03 06:02:35

标签: ios objective-c uitextfield uitextfielddelegate uimenucontroller

我在一个类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 

任何人都可以告诉我在我的代码中犯了什么错误。

5 个答案:

答案 0 :(得分:8)

在您的代码中添加以下方法,

<li class="list-group-item">
<table border="0">
<tr style="height: 40px">
<td style="width: 80px">Start Date</td>
<td>
<div class="input-group date" id="datepicker">
<input id="startDate" class="form-control" type="text" ng-model="list_S_date_Selected">
<span class="input-group-addon"><i class="fa fa-calendar"></i></span>
</div>
</td>
</tr>
</table>
<table border="0">
<tr style="height: 40px">
<td style="width: 80px">End Date</td>
<td>
<div class="input-group date" id="datepicker2">
<input id="endDate" class="form-control" type="text" ng-model="list_E_date_Selected">
<span class="input-group-addon"><i class="fa fa-calendar"></i>
</span>
</div>
</td>
</table>
</li>

它将禁用所有类型的编辑。

希望这会有所帮助:)

答案 1 :(得分:2)

如果您只想将其应用于密码等特殊文本字段:

- (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;
}

希望它有所帮助。

答案 2 :(得分:1)

-canPerformAction:withSender:应该在UITextField的子类中。它看起来不像你的子类,因为你正在分配一个UITextField。

答案 3 :(得分:0)

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];
}

答案 4 :(得分:0)

快捷键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)
}
相关问题