有时候在开发中要求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: // 往左缩进
所以我们通过继承UITextField的方法来实现canPerformAction的操作
#import "XJNoActionTextField.h"
@implementation XJNoActionTextField
- (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];
}
网友评论