禁用在UITextView中剪切和复制,即使允许用户编辑文本
问题描述:
我有一个 UITextView
可以编辑。我有另一个按钮复制,所以我想禁用内置的复制和剪切功能的文本视图。
当在文本视图内双击时,它们显示为黑色的迷你popover。是否有任何方法只阻止这两个选项,仍然让用户编辑文本?
I have a UITextView
which can be edited. I have another button for "Copy", so I want to disable the built-in "Copy" and "Cut" features of the text view.
These are shown as a black mini popover when double tapping inside the text view. Is there any way to block only these two options and still let the user edit the text?
答
覆盖 canPerformAction:withSender:
方法为您不想允许的操作返回 NO
:
overrides the canPerformAction:withSender:
method to return NO
for actions that you don't want to allow:
- (BOOL)canPerformAction:(SEL)action withSender:(id)sender
{
if (action == @selector(paste:))
return NO;
if (action == @selector(select:))
return NO;
if (action == @selector(selectAll:))
return NO;
return [super canPerformAction:action withSender:sender];
}
另一种方式 b
-(BOOL)canPerformAction:(SEL)action withSender:(id)sender {
UIMenuController *menuController = [UIMenuController sharedMenuController];
if (menuController) {
[UIMenuController sharedMenuController].menuVisible = NO;
}
return NO;
}
同时检查此链接