如何在UITextView中禁用复制,剪切,选择,全选

Ais*_*rya 106 objective-c uitextview ios

UITextView的复制,剪切,选择,全选功能在默认情况下,当我按下屏幕上显示.但是,在我的项目中,UITextField它只是只读.我不需要这个功能.请告诉我如何禁用此功能.

rpe*_*ich 103

禁用粘贴板操作的最简单方法是创建一个子类,UITextView该子类重写该canPerformAction:withSender:方法以返回NO您不想允许的操作:

- (BOOL)canPerformAction:(SEL)action withSender:(id)sender
{
    if (action == @selector(paste:))
        return NO;
    return [super canPerformAction:action withSender:sender];
}
Run Code Online (Sandbox Code Playgroud)

另见UIResponder

  • ` - (BOOL)canPerformAction:(SEL)action withSender:(id)sender {return NO; } - 阻止所有选项 (2认同)

小智 67

子类UITextView并覆盖canBecomeFirstResponder:

- (BOOL)canBecomeFirstResponder {
    return NO;
}
Run Code Online (Sandbox Code Playgroud)

请注意,这仅适用于不可编辑的UITextViews!没有在可编辑的上测试它...


Dam*_*bin 29

如果要在所有 UITextView应用程序上禁用剪切/复制/粘贴,可以使用以下类别:

@implementation UITextView (DisableCopyPaste)

- (BOOL)canBecomeFirstResponder
{
    return NO;
}

@end
Run Code Online (Sandbox Code Playgroud)

它保存了子类...... :-)

  • 您无法使用类别安全地覆盖方法.这是未定义的行为.您必须子类才能安全地覆盖方法. (16认同)
  • 这仅作为副作用,并防止`UITextView`按预期行为,例如,当它可编辑并触摸时.重写`canPerformAction:withSender:`; 这就是协议的用途. (4认同)
  • 你也可以把它只放在需要这种行为的/,文件中. (3认同)
  • 注意:这将适用于您的应用程序中的所有UITextView.大部分时间都不理想. (2认同)
  • 另请注意Apple [反对此建议](https://developer.apple.com/library/ios/documentation/Cocoa/Conceptual/ProgrammingWithObjectiveC/CustomizingExistingClasses/CustomizingExistingClasses.html#//apple_ref/doc/uid/TP40011210-CH6- SW4):"如果在类别中声明的方法的名称与原始类中的方法相同,或者在同一个类(或甚至是超类)中的另一个类别中的方法相同,则行为未定义为哪个方法实现在运行时使用... [和]在使用类别向标准Cocoa或Cocoa Touch类添加方法时会导致问题." (2认同)

小智 29

这对我来说是最好的解决方案:

UIView *overlay = [[UIView alloc] init];  
[overlay setFrame:CGRectMake(0, 0, myTextView.contentSize.width, myTextView.contentSize.height)];  
[myTextView addSubview:overlay];  
[overlay release];
Run Code Online (Sandbox Code Playgroud)

来自:https://stackoverflow.com/a/5704584/1293949

  • 我喜欢这里的胶带和打包线方法.我希望我能再次为你+1 :)相反,这里有一位金星:http://i.imgur.com/EXLFt1Z.jpg (7认同)
  • 好的,没有子类在这里! (3认同)

Luk*_*ath 20

如果您不需要UITextView滚动,那么不涉及子类的最简单的解决方案是简单地禁用文本视图的用户交互:

textField.userInteractionEnabled = NO;
Run Code Online (Sandbox Code Playgroud)

  • 好吧,我原以为这很明显. (3认同)
  • 如果这是文本视图中的内容,这会消除链接等.需要注意的是,这不是一个好的解决方案,想要隐藏选择/复制/粘贴,但也保持一定程度的交互启用. (2认同)

pin*_*nch 18

@rpetrich的回答对我有用.我发布扩展代码,以防它节省一些时间.

在我的情况下,我不想要任何弹出窗口,但我确实希望UITextField能够成为第一响应者.

不幸的是,当您点按并按住文本字段时,您仍会获得放大镜弹出窗口.

@interface NoSelectTextField : UITextField

@end

@implementation NoSelectTextField

- (BOOL)canPerformAction:(SEL)action withSender:(id)sender {
    if (action == @selector(paste:) ||
        action == @selector(cut:) ||
        action == @selector(copy:) ||
        action == @selector(select:) ||
        action == @selector(selectAll:) ||
        action == @selector(delete:) ||
        action == @selector(makeTextWritingDirectionLeftToRight:) ||
        action == @selector(makeTextWritingDirectionRightToLeft:) ||
        action == @selector(toggleBoldface:) ||
        action == @selector(toggleItalics:) ||
        action == @selector(toggleUnderline:)
        ) {
            return NO;
    }
    return [super canPerformAction:action withSender:sender];
}

@end
Run Code Online (Sandbox Code Playgroud)

斯威夫特4

class NoSelectTextField: UITextField {

    override func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool {
        if action == #selector(paste(_:)) ||
            action == #selector(cut(_:)) ||
            action == #selector(copy(_:)) ||
            action == #selector(select(_:)) ||
            action == #selector(selectAll(_:)) ||
            action == #selector(delete(_:)) ||
            action == #selector(makeTextWritingDirectionLeftToRight(_:)) ||
            action == #selector(makeTextWritingDirectionRightToLeft(_:)) ||
            action == #selector(toggleBoldface(_:)) ||
            action == #selector(toggleItalics(_:)) ||
            action == #selector(toggleUnderline(_:)) {
            return false
        }
        return super.canPerformAction(action, withSender: sender)
    }

}
Run Code Online (Sandbox Code Playgroud)


小智 15

最简单的方法是创建一个覆盖canPerformAction的UITextView子类:withSender:

- (BOOL)canPerformAction:(SEL)action withSender:(id)sender    
{    
     [UIMenuController sharedMenuController].menuVisible = NO;  //do not display the menu
     [self resignFirstResponder];                      //do not allow the user to selected anything
     return NO;
}
Run Code Online (Sandbox Code Playgroud)


Ada*_*ner 13

当我在iOS 7上的canPerformAction中返回NO时,我会收到很多这样的错误:

<Error>: CGContextSetFillColorWithColor: invalid context 0x0. This is a serious error. This application, or a library it uses, is using an invalid context and is thereby contributing to an overall degradation of system stability and reliability. This notice is a courtesy: please fix this problem. It will become a fatal error in an upcoming update.

我的解决方案如下:

- (BOOL)canPerformAction:(SEL)action withSender:(id)sender {
    [[NSOperationQueue mainQueue] addOperationWithBlock:^{
        [[UIMenuController sharedMenuController] setMenuVisible:NO animated:NO];
    }];
    return [super canPerformAction:action withSender:sender];
}
Run Code Online (Sandbox Code Playgroud)

诀窍是在主队列的下一个循环中隐藏菜单控制器(就在它显示之后).


GL7*_*777 10

这是在UITextView中禁用整个选择/复制/粘贴菜单的最简单方法

-(BOOL)canPerformAction:(SEL)action withSender:(id)sender
{    
    [UIMenuController sharedMenuController].menuVisible = NO;
    return NO;    
}
Run Code Online (Sandbox Code Playgroud)


Khu*_*ong 6

您可以通过取消选中这些框来修复故事板中的此问题:

在此输入图像描述

或者您可以像这样以编程方式设置:

textView.selectable = false
textView.editable = false
Run Code Online (Sandbox Code Playgroud)


小智 5

如果您想用 a 替换键盘,比如说,UIPicker作为inputView(当然还有一个工具栏作为inputAccesotyView),那么这个解决方法可能会有所帮助......

  • 实施 textFieldShouldBeginEditing:
  • 里面放 textField.userInteractionEnabled = NO;
  • 然后,当您要关闭 时UIPickerView,将其设置为 YES。

通过这样做,您将能够点击UITextField并显示从 中进行选择的选项UIPickerView,此时您UITextField确实不会对任何触摸事件做出反应(这包括触摸并按住以进行剪切、复制和粘贴)。但是,您必须记住在关闭时将其设置回“是”,UIPickerView但是您将无法UIPickerView再次访问。

唯一失败的时刻是当用户通过点击并按住 开始时UITextView,您会第一次再次看到剪切复制和粘贴。这就是为什么您应该始终验证您的输入。这是我能想到的最简单的方法。另一种选择是将 aUILabel用于只读文本,但您错过了UITextView.


Man*_*ios 5

子类 UITextView - swift 4.0

     override public func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool {
        return false
    }
Run Code Online (Sandbox Code Playgroud)


Jay*_*nki 5

如果您想禁用弹出窗口,请UITextField尝试使用此UITextFieldDelegate方法切换isUserInteractionEnabled.

func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {
    textField.isUserInteractionEnabled = false
    return true
}
func textFieldShouldEndEditing(_ textField: UITextField) -> Bool {
    textField.isUserInteractionEnabled = true
    return true
}
Run Code Online (Sandbox Code Playgroud)