美文网首页iOS 学习iOS开发知识小集
iOS UITextField中禁止复制粘贴

iOS UITextField中禁止复制粘贴

作者: 零粹 | 来源:发表于2019-02-22 09:51 被阅读2次

      UITextField中有一个方法:

    -(BOOL)canPerformAction:(SEL)action withSender:(id)sender
    

      能够控制用户对输入框进行哪种操作:

    cut: // 剪切
    copy: // 拷贝
    select: // 选择
    selectAll: // 全选
    paste: // 粘贴
    delete: // 删除
    _promptForReplace: // Replace...
    _transliterateChinese: // 简<=>繁
    _showTextStyleOptions: // B/<u>U</u>
    _define: // Define
    _addShortcut: // Learn...
    _accessibilitySpeak: // Speak
    _accessibilitySpeakLanguageSelection: // Speak...
    _accessibilityPauseSpeaking: // Pause
    _share: // 共享...
    makeTextWritingDirectionRightToLeft: // 往右缩进
    makeTextWritingDirectionLeftToRight: // 往左缩进
    

    可以看出有很多操作都是默认开放的。如果需要禁用默写操作建议整体禁用,或者你需要用到哪些操作,就针对这些操作开放,其他的都禁止了。

    代码如下:

    //建议整体禁用(复制、粘贴、全选等都被禁用了)
    -(BOOL)canPerformAction:(SEL)action withSender:(id)sender{
    
        UIMenuController *menuController = [UIMenuController sharedMenuController];
        if (menuController) {
            [UIMenuController sharedMenuController].menuVisible = NO;
        }
        return NO;
    }
    
    //选择我们需要的权限打开 - 这样可以有效的控制不必要的权限使用
    - (BOOL)canPerformAction:(SEL)action withSender:(id)sender
    {
        //此处我们选择打开粘贴功能、其他功能都禁用
        if (action == @selector(paste:))
            return YES;
        return NO;
    }
    

    注意:需要写UITextField的分类重写canPerformAction方法,再针对输入框需要的操作打开对应的权限。

    demo代码如下:

    #import "UITextField+SET.h"
    
    @implementation UITextField (SET)
    
    //选择我们需要的权限打开 - 这样可以有效的控制不必要的权限使用
    - (BOOL)canPerformAction:(SEL)action withSender:(id)sender
    {
        
        if (self.tag == 100) {
            //此处我们选择打开粘贴功能、其他功能都禁用
            if (action == @selector(paste:))
                return YES;
        }else if (self.tag == 101){
            //此处我们选择打开选择、全选、复制功能、其他功能都禁用
            if (action == @selector(copy:) || action == @selector(select:) || action == @selector(selectAll:))
                return YES;
        }
        
        return NO;
    }
    
    
    @end
    
    #import "ViewController.h"
    
    @interface ViewController ()<UITextFieldDelegate>
    
    @property (nonatomic, strong) UITextField *pasteField;
    
    @property (nonatomic, strong) UITextField *noneField;
    
    @end
    
    @implementation ViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        // Do any additional setup after loading the view, typically from a nib.
        
        self.pasteField = [[UITextField alloc] initWithFrame:CGRectMake(20, 100, 200, 30)];
        self.pasteField.layer.borderWidth = 0.5f;
        self.pasteField.layer.borderColor = [UIColor blackColor].CGColor;
        self.pasteField.placeholder = @"只允许粘贴";
        self.pasteField.delegate = self;
        self.pasteField.tag = 100;
        [self.view addSubview:self.pasteField];
        
        self.noneField = [[UITextField alloc] initWithFrame:CGRectMake(20, 200, 200, 30)];
        self.noneField.layer.borderWidth = 0.5f;
        self.noneField.layer.borderColor = [UIColor blackColor].CGColor;
        self.noneField.placeholder = @"只允许选择、全选、复制";
        self.noneField.delegate = self;
        self.noneField.tag = 101;
        [self.view addSubview:self.noneField];
    }
    
    @end
    
    

    demo git地址:https://github.com/ITHanYong/UITextField-banCopyPaste.git

    相关文章

      网友评论

        本文标题:iOS UITextField中禁止复制粘贴

        本文链接:https://www.haomeiwen.com/subject/ifssyqtx.html