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

Remove "Copy" menu item from programatically created UITextView IOS

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

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

- (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];
}

为了更好地查看问题,如果 selector 实际上考虑了 "Copy" 选项,我尝试注销,这是我的日志:

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",因为日志显示 "It went in canPerform",但它无法识别 "copy" 操作,因为日志从未显示:"It prevented the copy option and hid it from the menu".
因此,我的问题是我需要从 selected 文本菜单中隐藏 "Copy" 项。我错过了什么?

试试这个

UIMenuItem *textItem1 = [[UIMenuItem alloc] initWithTitle:NSLocalizedString(@"Start", nil) action:@selector(start:)];
UIMenuItem *textItem2 = [[UIMenuItem alloc] initWithTitle:NSLocalizedString(@"Reply", nil) action:@selector(reply:)];

UIMenuItem *textItem3 = [[UIMenuItem alloc] initWithTitle:NSLocalizedString(@"Report Abuse", nil) action:@selector(startreport:)];

[UIMenuController sharedMenuController].menuItems = @[textItem1, textItem2, textItem3];



[[UIMenuController sharedMenuController] setTargetRect:cell.imgBg.frame inView:cell.contentView];
[[UIMenuController sharedMenuController] setMenuVisible:YES animated:YES];

canPerformAction(_:withSender:) 是一个 UIResponder 方法,因此如果您在 viewController 中实现它,它只会在 viewController 上调用 copy: 时触发。文档说:"Requests the receiving responder to enable or disable the specified command in the user interface."

您必须继承 UITextView 并在子类中实现它,我已经为您创建了一个简单的 gist

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

1 - It 仅在您以编程方式创建 UITextView 时才有效,如果您使用故事板创建它,它将无效,这是最重要的部分,因为如果您有一个已经使用故事板创建了 UITextView,您将无法删除 "copy" ,您将能够删除其他所有内容!只是不是副本

2 - 对于所有新开发人员,您必须子class UITextView class,这是通过创建一个新的 class 并将其选为子 class 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 - 设置完所有内容后,棘手的部分就来了。在 class 你想使用 UITextView,当然包括你的 customClass,然后,这就是你调用你的 textView 的方式:

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

其次,在您完成所有工作的实际 class 中,转到您的 .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;

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