草庐IT

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

coder 2024-01-24 原文

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

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

- (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 - 从以编程方式创建的 UITextView IOS 中删除 "Copy"菜单项的更多相关文章

  1. ruby - 如何在 Ruby 中顺序创建 PI - 2

    出于纯粹的兴趣,我很好奇如何按顺序创建PI,而不是在过程结果之后生成数字,而是让数字在过程本身生成时显示。如果是这种情况,那么数字可以自行产生,我可以对以前看到的数字实现垃圾收集,从而创建一个无限系列。结果只是在Pi系列之后每秒生成一个数字。这是我通过互联网筛选的结果:这是流行的计算机友好算法,类机器算法:defarccot(x,unity)xpow=unity/xn=1sign=1sum=0loopdoterm=xpow/nbreakifterm==0sum+=sign*(xpow/n)xpow/=x*xn+=2sign=-signendsumenddefcalc_pi(digits

  2. python - 如何使用 Ruby 或 Python 创建一系列高音调和低音调的蜂鸣声? - 2

    关闭。这个问题是opinion-based.它目前不接受答案。想要改进这个问题?更新问题,以便editingthispost可以用事实和引用来回答它.关闭4年前。Improvethisquestion我想在固定时间创建一系列低音和高音调的哔哔声。例如:在150毫秒时发出高音调的蜂鸣声在151毫秒时发出低音调的蜂鸣声200毫秒时发出低音调的蜂鸣声250毫秒的高音调蜂鸣声有没有办法在Ruby或Python中做到这一点?我真的不在乎输出编码是什么(.wav、.mp3、.ogg等等),但我确实想创建一个输出文件。

  3. ruby-on-rails - rails : "missing partial" when calling 'render' in RSpec test - 2

    我正在尝试测试是否存在表单。我是Rails新手。我的new.html.erb_spec.rb文件的内容是:require'spec_helper'describe"messages/new.html.erb"doit"shouldrendertheform"dorender'/messages/new.html.erb'reponse.shouldhave_form_putting_to(@message)with_submit_buttonendendView本身,new.html.erb,有代码:当我运行rspec时,它失败了:1)messages/new.html.erbshou

  4. ruby-on-rails - 由于 "wkhtmltopdf",PDFKIT 显然无法正常工作 - 2

    我在从html页面生成PDF时遇到问题。我正在使用PDFkit。在安装它的过程中,我注意到我需要wkhtmltopdf。所以我也安装了它。我做了PDFkit的文档所说的一切......现在我在尝试加载PDF时遇到了这个错误。这里是错误:commandfailed:"/usr/local/bin/wkhtmltopdf""--margin-right""0.75in""--page-size""Letter""--margin-top""0.75in""--margin-bottom""0.75in""--encoding""UTF-8""--margin-left""0.75in""-

  5. ruby - 如何以所有可能的方式将字符串拆分为长度最多为 3 的连续子字符串? - 2

    我试图获取一个长度在1到10之间的字符串,并输出将字符串分解为大小为1、2或3的连续子字符串的所有可能方式。例如:输入:123456将整数分割成单个字符,然后继续查找组合。该代码将返回以下所有数组。[1,2,3,4,5,6][12,3,4,5,6][1,23,4,5,6][1,2,34,5,6][1,2,3,45,6][1,2,3,4,56][12,34,5,6][12,3,45,6][12,3,4,56][1,23,45,6][1,2,34,56][1,23,4,56][12,34,56][123,4,5,6][1,234,5,6][1,2,345,6][1,2,3,456][123

  6. ruby - 解析 RDFa、微数据等的最佳方式是什么,使用统一的模式/词汇(例如 schema.org)存储和显示信息 - 2

    我主要使用Ruby来执行此操作,但到目前为止我的攻击计划如下:使用gemsrdf、rdf-rdfa和rdf-microdata或mida来解析给定任何URI的数据。我认为最好映射到像schema.org这样的统一模式,例如使用这个yaml文件,它试图描述数据词汇表和opengraph到schema.org之间的转换:#SchemaXtoschema.orgconversion#data-vocabularyDV:name:namestreet-address:streetAddressregion:addressRegionlocality:addressLocalityphoto:i

  7. ruby - 使用 Vim Rails,您可以创建一个新的迁移文件并一次性打开它吗? - 2

    使用带有Rails插件的vim,您可以创建一个迁移文件,然后一次性打开该文件吗?textmate也可以这样吗? 最佳答案 你可以使用rails.vim然后做类似的事情::Rgeneratemigratonadd_foo_to_bar插件将打开迁移生成的文件,这正是您想要的。我不能代表textmate。 关于ruby-使用VimRails,您可以创建一个新的迁移文件并一次性打开它吗?,我们在StackOverflow上找到一个类似的问题: https://sta

  8. ruby - 检查 "command"的输出应该包含 NilClass 的意外崩溃 - 2

    为了将Cucumber用于命令行脚本,我按照提供的说明安装了arubagem。它在我的Gemfile中,我可以验证是否安装了正确的版本并且我已经包含了require'aruba/cucumber'在'features/env.rb'中为了确保它能正常工作,我写了以下场景:@announceScenario:Testingcucumber/arubaGivenablankslateThentheoutputfrom"ls-la"shouldcontain"drw"假设事情应该失败。它确实失败了,但失败的原因是错误的:@announceScenario:Testingcucumber/ar

  9. ruby-on-rails - 无法使用 Rails 3.2 创建插件? - 2

    我对最新版本的Rails有疑问。我创建了一个新应用程序(railsnewMyProject),但我没有脚本/生成,只有脚本/rails,当我输入ruby./script/railsgeneratepluginmy_plugin"Couldnotfindgeneratorplugin.".你知道如何生成插件模板吗?没有这个命令可以创建插件吗?PS:我正在使用Rails3.2.1和ruby​​1.8.7[universal-darwin11.0] 最佳答案 随着Rails3.2.0的发布,插件生成器已经被移除。查看变更日志here.现在

  10. ruby - 如何使用 RSpec::Core::RakeTask 创建 RSpec Rake 任务? - 2

    如何使用RSpec::Core::RakeTask初始化RSpecRake任务?require'rspec/core/rake_task'RSpec::Core::RakeTask.newdo|t|#whatdoIputinhere?endInitialize函数记录在http://rubydoc.info/github/rspec/rspec-core/RSpec/Core/RakeTask#initialize-instance_method没有很好的记录;它只是说:-(RakeTask)initialize(*args,&task_block)AnewinstanceofRake

随机推荐