我正在制作一个注册警报视图,其中包含一个UITextField,用户可以在其中输入注册号。一切都是他们的,但我想以编程方式从文本字段中删除复制粘贴功能,因为他们不是文本字段的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];
}
答案 0 :(得分:46)
这篇文章有许多不错的解决方案:How disable Copy, Cut, Select, Select All in UITextView
我最喜欢的是覆盖canPerformAction:withSender:
:
- (BOOL)canPerformAction:(SEL)action withSender:(id)sender
{
if (action == @selector(paste:))
return NO;
return [super canPerformAction:action withSender:sender];
}
答案 1 :(得分:22)
对于iOS8.0 +,Xcode 6.0.1,启用了ARC
希望像我一样保存一个初学者,一段时间实现这个......
实现禁用复制/粘贴/剪切/等。你必须继承UITextField并覆盖......
- (BOOL)canPerformAction:(SEL)action withSender:(id)sender
要做到这一点......
创建一个新类,它是UITextField的子类(即要包含在app文件夹中的新.h和.m文件)。所以File-> New->“Cocoa Touch Class” - > Next->“PasteOnlyUITextField”(例如),“UITextField”的子类 - > Next-> Create。
为我们新的UITextField子类创建.h和.m文件,名为“PasteOnlyUITextField”......
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"
现在您必须使用子类,或者使用身份检查器
PasteOnlyUITextField *pasteOnlyUITextField = [[PasteOnlyUITextField alloc] init...];
...或
选择UITextField并转到身份检查器,选择其类。
您可以根据需要更改与菜单选项关联的逻辑...
希望这有帮助!感谢所有原始贡献者。
答案 2 :(得分:22)
我找到了一种方法,使用swift使用扩展和没有子类化的associatedObject。 我使用readonly属性来禁用粘贴/剪切,但可以调整此样本。
Swift 3 自2016年11月27日起更新
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)
}
}
答案 3 :(得分:19)
Storyboard用户可能希望查看此解决方案,只要您可以使用子类化。
我认为通过扩展或协议可以轻松实现这一目标。
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 : this is not correct
return super.canPerformAction(action, withSender: sender)
}
}
}
答案 4 :(得分:12)
在ViewController.m中实现此方法此方法将帮助您禁用UITextField
上的选项。
它包含对应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];
}
答案 5 :(得分:8)
在iOS 9中,我们可以隐藏键盘上的复制粘贴栏
-(void) customMethod{
yourTextField.inputAssistantItem.leadingBarButtonGroups = @[];
yourTextField.inputAssistantItem.trailingBarButtonGroups = @[];
}
答案 6 :(得分:2)
iOS 10及更早版本(Swift 3)的this answer小更新:
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)
}
答案 7 :(得分:1)
如果禁用的文字选择适用于您,请尝试此操作。
class NoMoreSelectionTextField: UITextField {
override func caretRect(for position: UITextPosition) -> CGRect {
return CGRect.zero
}
override var selectedTextRange: UITextRange? {
get { return nil }
set { return }
}
}
答案 8 :(得分:1)
在viewController中尝试
- (BOOL)canPerformAction:(SEL)action withSender:(id)sender {
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
[[UIMenuController sharedMenuController] setMenuVisible:NO animated:NO];
}];
return [super canPerformAction:action withSender:sender];
}
答案 9 :(得分:1)
注意:在Swift中,如果您希望您的文本字段禁用所有UIResponderStandardEditActions
(剪切,复制,粘贴,查找,共享,选择),请使用UITextFieldDelegate
。
func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {
textField.isUserInteractionEnabled = false
return true
}
func textFieldDidEndEditing(_ textField: UITextField) {
textField.isUserInteractionEnabled = true
}
答案 10 :(得分:1)
Swift 5解决方案:
[items]="customers | async"
答案 11 :(得分:0)
你可以在swift中扩展textview或textfield,如下所示:
extension UITextView {
open override func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool {
return false
}
}
答案 12 :(得分:-1)
覆盖targetForAction:withSender是最好的恕我直言:
- (id)targetForAction:(SEL)action withSender:(id)sender
{
if (action == @selector(paste:)) {
return nil;
}
return [super targetForAction:action withSender:sender];
}
答案 13 :(得分:-1)
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)
}
}
答案 14 :(得分:-3)
用于iOS 7或更高版本
- (BOOL)canPerformAction:(SEL)action withSender:(id)sender {
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
[[UIMenuController sharedMenuController] setMenuVisible:NO animated:NO];
}];
return [super canPerformAction:action withSender:sender];
}
答案 15 :(得分:-35)
只需设置userInteractionEnabled = NO;