ios - 从以编程方式创建的 UITextView IOS 中删除 "Copy"菜单项

标签 ios uitextview uimenuitem

我在我的 viewDidLoad 中以编程方式创建我的 UITextView。
当我选择一个文本时,菜单会显示以下内容:

enter image description here

如图所示,我添加了两个自定义按钮,突出显示和取消突出显示。我想删除“复制”选项并保留所有其他选项,所以我不能使其不可编辑,我需要允许用户从文本中选择他想要的任何内容,但要防止它复制内容。
我尝试了几种方法,包括整个社区都提到的这个方法:

- (BOOL)canPerformAction:(SEL)action withSender:(id)sender
{
     NSLog(@"it went in canPerform");
     if (action == @selector(copy:)){

          NSLog(@"It prevented the copy option and hid it from the menu");
          return NO;

     }
     return [super canPerformAction:action withSender:sender];
}

为了更好地查看问题,如果选择器实际上考虑了“复制”选项,我尝试注销,这是我的日志:

2015-05-20 11:27:47.637 MyApp[56604:11786730] it went in canPerform

2015-05-20 11:27:47.637 MyApp[56604:11786730] it went in canPerform

2015-05-20 11:27:47.638 MyApp[56604:11786730] it went in canPerform

2015-05-20 11:27:47.638 MyApp[56604:11786730] it went in canPerform

2015-05-20 11:27:47.638 MyApp[56604:11786730] it went in canPerform

2015-05-20 11:27:47.638 MyApp[56604:11786730] it went in canPerform

2015-05-20 11:27:47.638 MyApp[56604:11786730] it went in canPerform

2015-05-20 11:27:47.638 MyApp[56604:11786730] it went in canPerform

2015-05-20 11:27:47.663 MyApp[56604:11786730] it went in canPerform

2015-05-20 11:27:47.664 MyApp[56604:11786730] it went in canPerform

2015-05-20 11:27:47.664 MyApp[56604:11786730] it went in canPerform

2015-05-20 11:27:47.664 MyApp[56604:11786730] it went in canPerform

2015-05-20 11:27:47.664 MyApp[56604:11786730] it went in canPerform

2015-05-20 11:27:47.664 MyApp[56604:11786730] it went in canPerform

2015-05-20 11:27:47.664 MyApp[56604:11786730] it went in canPerform

2015-05-20 11:27:47.664 MyApp[56604:11786730] it went in canPerform

如图所示,代码正在调用“canPerformAction”,因为日志显示“它进入了 canPerform”,但它无法识别“复制”操作,因为日志从未显示:“它阻止了复制选项并隐藏了它从菜单”。
因此,我的问题是我需要从选定的文本菜单中隐藏“复制”项。我错过了什么?

最佳答案

对于所有必须处理这种情况的 future 开发人员,我知道你会多么沮丧,所以我会提供一个完整的教程,因为我遇到了结合了大多数已发布答案的正确答案:

1 - 仅当您以编程方式创建 UITextView 时才有效,如果您使用 Storyboard创建它,它将无效,这是最重要的部分,因为如果您已经创建了 UITextView使用 Storyboard ,您将无法删除“复制”,您将能够删除其他所有内容!只是不是副本

2 - 您必须继承 UITextView 类,对于所有新开发人员,这是通过创建一个新类并选择它作为 UITextView 的子类来完成的。

3 - 您的 .h 文件看起来像这样

//
//  myCustomClass.h
//  myApp
//
//  Created by Elias Rahme on 5/20/15.
//  Copyright (c) 2015 Elias Rahme. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface myCustomClass : UITextView

@end

你的 .m 文件将是这样的:

//
//  myCustomClass.m
//  myApp
//
//  Created by Elias Rahme on 5/20/15.
//  Copyright (c) 2015 Elias Rahme. All rights reserved.
//

#import "myCustomClass.h"

@implementation myCustomClass


-(BOOL)canPerformAction:(SEL)action withSender:(id)sender {
     if (action == @selector(copy:)) {
          return NO;
     }
     return [super canPerformAction:action withSender:sender];
}
@end

4 - 设置完所有内容后,棘手的部分就来了。在您想使用 UITextView 的类中,当然包括您的 customClass,然后,这就是您调用 textView 的方式:

首先,您必须创建一个新的 UITextView,这是通过在实现上方编写 UITextView *newTextView; 来完成的,就像您创建任何变量一样。

其次,在您完成所有工作的实际类里面,转到您的 .h 文件,并确保它看起来像这样

#import <UIKit/UIKit.h>

@interface myActuallClassWhereIWantTheActualWorkToBeDone : UIViewController< UITextViewDelegate>{


}

第三,将以下行添加到您的 viewDidLoad 方法中:

 newTextView = [[myCustomClass alloc] initWithFrame:CGRectMake(yourX, yourY, yourWidth, yourHeight)];
newTextView.text = @"This is the best small tutorial EVER!!";
 newTextView.delegate = self;

好了,运行您的应用程序,您将看不到“复制”选项!这是一个简单而棘手的过程,我花了几个小时才真正总结了我访问过的所有网页!希望大家喜欢!

关于ios - 从以编程方式创建的 UITextView IOS 中删除 "Copy"菜单项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30344373/

相关文章:

ios - swift 不能在 firebase 数据库中创建一个 child

ios - 在 UITextView 中编辑文本时添加不同的颜色,而不管 Swift 中的光标位置如何

ios - UITextView 文字背景色

ios - 具有相同选择器的多个 UIMenuItem

ios - iOS 中的 Facebook 身份验证错误

ios - 在 iOS 中使用不带 `awsconfiguration.json` 的 AWSMobileClient

ios - 使用等宽数字系统字体的 UITextField 的错误 intrinsicContentSize(宽度)导致剪切文本

ios - 可选择 UITextView 链接,而无需选择其余文本

ios - 无法在 UITextView 的 UIMenuController 中禁用默认 UIMenuItems