This question already has an answer here:
这个问题在这里已有答案:
- How to disable copy paste option from UITextField programmatically 14 answers
- 如何以编程方式从UITextField禁用复制粘贴选项14个答案
UITextField how to disable the paste?
UITextField如何禁用粘贴?
1 个解决方案
#1
75
overrides the canPerformAction:withSender:
method to return NO
for actions that you don't want to allow:
覆盖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];
}
In Above Code you need to write only for paste
在上面的代码中,您只需要编写粘贴
Another way
其他方式
-(BOOL)canPerformAction:(SEL)action withSender:(id)sender {
UIMenuController *menuController = [UIMenuController sharedMenuController];
if (menuController) {
[UIMenuController sharedMenuController].menuVisible = NO;
}
return NO;
}
Also check This link
另请查看此链接
EDITED
EDITED
In iOS 7, you can do such like,,
在iOS 7中,你可以这样做,
- (BOOL)canPerformAction:(SEL)action withSender:(id)sender {
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
[[UIMenuController sharedMenuController] setMenuVisible:NO animated:NO];
}];
return [super canPerformAction:action withSender:sender];
}
For Swift User
对于Swift用户
override public func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool {
if action == #selector(copy(_:)) || action == #selector(paste(_:)) {
return false
}
return true
}
If you want to Open Date Picker or Picker view on TEXTFIELD click then below code work.
如果要在TEXTFIELD上打开日期选择器或选择器视图,请单击下面的代码工作。
Add below two methods in your class.
在您的课程中添加以下两种方法。
//Hide Menu View
override func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool {
if YOURTEXTFIELD.isFirstResponder {
DispatchQueue.main.async(execute: {
(sender as? UIMenuController)?.setMenuVisible(false, animated: false)
})
return false
}
return super.canPerformAction(action, withSender: sender)
}
//MUST Implement
//必须实施
func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {
return false
}
#1
75
overrides the canPerformAction:withSender:
method to return NO
for actions that you don't want to allow:
覆盖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];
}
In Above Code you need to write only for paste
在上面的代码中,您只需要编写粘贴
Another way
其他方式
-(BOOL)canPerformAction:(SEL)action withSender:(id)sender {
UIMenuController *menuController = [UIMenuController sharedMenuController];
if (menuController) {
[UIMenuController sharedMenuController].menuVisible = NO;
}
return NO;
}
Also check This link
另请查看此链接
EDITED
EDITED
In iOS 7, you can do such like,,
在iOS 7中,你可以这样做,
- (BOOL)canPerformAction:(SEL)action withSender:(id)sender {
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
[[UIMenuController sharedMenuController] setMenuVisible:NO animated:NO];
}];
return [super canPerformAction:action withSender:sender];
}
For Swift User
对于Swift用户
override public func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool {
if action == #selector(copy(_:)) || action == #selector(paste(_:)) {
return false
}
return true
}
If you want to Open Date Picker or Picker view on TEXTFIELD click then below code work.
如果要在TEXTFIELD上打开日期选择器或选择器视图,请单击下面的代码工作。
Add below two methods in your class.
在您的课程中添加以下两种方法。
//Hide Menu View
override func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool {
if YOURTEXTFIELD.isFirstResponder {
DispatchQueue.main.async(execute: {
(sender as? UIMenuController)?.setMenuVisible(false, animated: false)
})
return false
}
return super.canPerformAction(action, withSender: sender)
}
//MUST Implement
//必须实施
func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {
return false
}