UITextField如何禁用粘贴?
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]; }
在上面的代码中,您只需要编写粘贴
其他方式
-(BOOL)canPerformAction:(SEL)action withSender:(id)sender { UIMenuController *menuController = [UIMenuController sharedMenuController]; if (menuController) { [UIMenuController sharedMenuController].menuVisible = NO; } return NO; }
另外检查这个链接
EDITED
在iOS 7中,你可以像这样做,
- (BOOL)canPerformAction:(SEL)action withSender:(id)sender { [[NSOperationQueue mainQueue] addOperationWithBlock:^{ [[UIMenuController sharedMenuController] setMenuVisible:NO animated:NO]; }]; return [super canPerformAction:action withSender:sender]; }
对于Swift用户
override public func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool { if action == #selector(copy(_:)) || action == #selector(paste(_:)) { return false } return true }