禁用复制,粘贴到UITextfield中的操作在iOS 9.x中不起作用 [英] Disable copy, paste in UITextfield is not working in iOS 9.x

查看:111
本文介绍了禁用复制,粘贴到UITextfield中的操作在iOS 9.x中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一个类BBCustomUtility.h,.m类文件中创建了一个文本字段,然后

I created a text field in one class BBCustomUtility.h,.m class 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 ;
}

从普通类导入并在class1.m中使用

importing from common class and used 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 copy paste option on textfield in the below methods these are not working 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 any one please tell me what's the mistake i did in my code.

推荐答案

在代码中添加下面的方法,就是这样,

Add below method in your code and that`s it,

 - (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 edit.

希望这会有所帮助:)

这篇关于禁用复制,粘贴到UITextfield中的操作在iOS 9.x中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆