IOS之UITextField 禁止粘贴

时间:2021-08-04 09:10:09

方法1:重写 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];
}

方法2:

-(BOOL)canPerformAction:(SEL)action withSender:(id)sender {
UIMenuController *menuController = [UIMenuController sharedMenuController];
if (menuController) {
[UIMenuController sharedMenuController].menuVisible = NO;
}
return NO;
}