如何以编程方式从UITextField禁用复制粘贴选项
我正在做一个注册alertview有一个UITextField在用户可以input他们的注册号码。 一切都非常他们,但是我想从编程的文本域中删除复制粘贴function,因为他们是没有InterfaceBuilder版本的文本域我不知道如何做到这一点..
这里是我的UIalertview到目前为止…
- (void)pleaseRegisterDevice { UIAlertView *myAlertView = [[UIAlertView alloc] initWithTitle:@"Please Register Device!" message:@"this gets covered" delegate:self cancelButtonTitle:nil otherButtonTitles:@"OK", nil]; regTextField = [[UITextField alloc] initWithFrame:CGRectMake(12.0, 45.0, 260.0, 25.0)]; [regTextField setBackgroundColor:[UIColor whiteColor]]; regTextField.textAlignment = UITextAlignmentCenter; [myAlertView addSubview:regTextField]; [myAlertView show]; [myAlertView release]; }
这个post有很多很好的解决scheme: 如何禁用UITextView中的复制,剪切,select,全选
我最喜欢的是重写canPerformAction:withSender:
::
- (BOOL)canPerformAction:(SEL)action withSender:(id)sender { if (action == @selector(paste:)) return NO; return [super canPerformAction:action withSender:sender]; }
对于iOS8.0 +,启用了Xcode 6.0.1,ARC
希望能像我一样保存一个初学者,一些时间来实现这个…
执行禁用复制/粘贴/剪切/等。 你必须inheritanceUITextField并覆盖…
- (BOOL)canPerformAction:(SEL)action withSender:(id)sender
去做这个…
创build一个新的类,它是UITextField的一个子类(即一个新的.h和.m文件被包含在你的应用程序文件夹中)。 所以File-> New – >“Cocoa Touch Class” – > Next – >“PasteOnlyUITextField”(例如),“UITextField”的子类 – > Next – > Create。
一旦为我们称为“PasteOnlyUITextField”的新的UITextField子类创build了.h和.m文件…
PasteOnlyUITextField.h
#import <UIKit/UIKit.h> @interface PasteOnlyUITextField : UITextField @end
PasteOnlyUITextField.m
#import "PasteOnlyUITextField.h" @implementation PasteOnlyUITextField /* // Only override drawRect: if you perform custom drawing. // An empty implementation adversely affects performance during animation. - (void)drawRect:(CGRect)rect { // Drawing code } */ - (BOOL)canPerformAction:(SEL)action withSender:(id)sender { if (action == @selector(paste:)) { return true; } return false; } @end
现在确保你导入PasteOnlyUITextField.h你要使用它,例如YourUIViewController.h文件…
#import "PasteOnlyUITextField.h"
现在,您必须使用子类,或者使用progrommatically或使用身份检查器
PasteOnlyUITextField *pasteOnlyUITextField = [[PasteOnlyUITextField alloc] init...];
要么…
selectUITextField并转到身份检查器,select其类。
您可以根据需要更改与菜单选项关联的逻辑…
希望这可以帮助! 感谢所有原始贡献者。
我已经find了使用扩展和关联的对象而不是子类的swift方法。 我只使用一个属性来禁用粘贴/剪切,但是这个样本可以被修改。
Swift 3截至27/11/2016更新
var key: Void? class UITextFieldAdditions: NSObject { var readonly: Bool = false } extension UITextField { var readonly: Bool { get { return self.getAdditions().readonly } set { self.getAdditions().readonly = newValue } } private func getAdditions() -> UITextFieldAdditions { var additions = objc_getAssociatedObject(self, &key) as? UITextFieldAdditions if additions == nil { additions = UITextFieldAdditions() objc_setAssociatedObject(self, &key, additions!, objc_AssociationPolicy.OBJC_ASSOCIATION_RETAIN) } return additions! } open override func target(forAction action: Selector, withSender sender: Any?) -> Any? { if ((action == #selector(UIResponderStandardEditActions.paste(_:)) || (action == #selector(UIResponderStandardEditActions.cut(_:)))) && self.readonly) { return nil } return super.target(forAction: action, withSender: sender) } }
其他Swift(2.2)
import UIKit var key: Void? class UITextFieldAdditions: NSObject { var readonly: Bool = false } extension UITextField { var readonly: Bool { get { return self.getAdditions().readonly } set { self.getAdditions().readonly = newValue } } private func getAdditions() -> UITextFieldAdditions { var additions = objc_getAssociatedObject(self, &key) as? UITextFieldAdditions if additions == nil { additions = UITextFieldAdditions() objc_setAssociatedObject(self, &key, additions!, objc_AssociationPolicy(OBJC_ASSOCIATION_RETAIN_NONATOMIC)) } return additions! } public override func targetForAction(action: Selector, withSender sender: AnyObject?) -> AnyObject? { if ((action == Selector("paste:") || (action == Selector("cut:"))) && self.readonly) { return nil } return super.targetForAction(action, withSender: sender) } }
在ViewController.m中实现此方法此方法将帮助您禁用UITextField
选项。
它包括粘贴,select,select所有和复制选项在你的对应的UITextField
。
这个方法在UITextField
情况下是非常有用的,当你想要这个密码或DateOfBirth
或任何你想要的。
- (BOOL)canPerformAction:(SEL)action withSender:(id)sender { if ((_TextField1 isFirstResponder] || [_TextFied2 isFirstResponder]) { [[NSOperationQueue mainQueue] addOperationWithBlock:^{ [[UIMenuController sharedMenuController] setMenuVisible:NO animated:NO]; }]; } return [super canPerformAction:action withSender:sender]; }
在iOS 9中,我们可以从键盘上隐藏复制粘贴栏
-(void) customMethod{ yourTextField.inputAssistantItem.leadingBarButtonGroups = @[]; yourTextField.inputAssistantItem.trailingBarButtonGroups = @[]; }
iOS 10及更早版本的这个答案的小更新(Swift 3):
open override func target(forAction action: Selector, withSender sender: Any?) -> Any? { guard isReadonly else { return super.target(forAction: action, withSender: sender) } if #available(iOS 10, *) { if action == #selector(UIResponderStandardEditActions.paste(_:)) { return nil } } else { if action == #selector(paste(_:)) { return nil } } return super.target(forAction: action, withSender: sender) }
故事板用户可能希望看看这个解决scheme,只要你可以inheritance子类。
我不认为有一个简单的方法来通过扩展或协议来实现这一点。
Swift 3.1
import UIKit @IBDesignable class CustomTextField: UITextField { @IBInspectable var isPasteEnabled: Bool = true @IBInspectable var isSelectEnabled: Bool = true @IBInspectable var isSelectAllEnabled: Bool = true @IBInspectable var isCopyEnabled: Bool = true @IBInspectable var isCutEnabled: Bool = true @IBInspectable var isDeleteEnabled: Bool = true override func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool { switch action { case #selector(UIResponderStandardEditActions.paste(_:)) where !isPasteEnabled, #selector(UIResponderStandardEditActions.select(_:)) where !isSelectEnabled, #selector(UIResponderStandardEditActions.selectAll(_:)) where !isSelectAllEnabled, #selector(UIResponderStandardEditActions.copy(_:)) where !isCopyEnabled, #selector(UIResponderStandardEditActions.cut(_:)) where !isCutEnabled, #selector(UIResponderStandardEditActions.delete(_:)) where !isDeleteEnabled: return false default: return true } } }
要点链接
你可以在swift中扩展textview或textfield,就像这样:
extension UITextView { open override func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool { return false } }
如果禁用的文本select适用于您,请尝试此操作。
class NoMoreSelectionTextField: UITextField { override func caretRect(for position: UITextPosition) -> CGRect { return CGRect.zero } override var selectedTextRange: UITextRange? { get { return nil } set { return } } }
尝试在您的viewController
- (BOOL)canPerformAction:(SEL)action withSender:(id)sender { [[NSOperationQueue mainQueue] addOperationWithBlock:^{ [[UIMenuController sharedMenuController] setMenuVisible:NO animated:NO]; }]; return [super canPerformAction:action withSender:sender]; }
用于iOS 7或更高版本
- (BOOL)canPerformAction:(SEL)action withSender:(id)sender { [[NSOperationQueue mainQueue] addOperationWithBlock:^{ [[UIMenuController sharedMenuController] setMenuVisible:NO animated:NO]; }]; return [super canPerformAction:action withSender:sender];
}
重写targetForAction:withSender是最好的恕我直言:
- (id)targetForAction:(SEL)action withSender:(id)sender { if (action == @selector(paste:)) { return nil; } return [super targetForAction:action withSender:sender]; }
Swift 3.0版本
class NoMenuTextField: UITextField { override func canPerformAction(action: Selector, withSender sender: AnyObject?) -> Bool { if (action == #selector(NSObject.paste(_:))) { return false } return super.canPerformAction(action, withSender: sender) } }
只要设置userInteractionEnabled = NO;