UITextField 禁用复制粘贴

2023-07-31  本文已影响0人  Dolway

创建UITextField的子类,重写canPerformAction:withSender:
返回NO表示不允许的操作:

- (BOOL)canPerformAction:(SEL)action withSender:(id)sender {
        if (action == @selector(paste:))//粘贴
           { return NO;}
        if (action == @selector(select:))// 选择   
           { return NO;}
        if (action == @selector(selectAll:))// 全选   
          {  return NO;}
        return [super canPerformAction:action withSender:sender];
}

所有可用操作action:

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;
}
上一篇下一篇

猜你喜欢

热点阅读