如何禁用Textview文本选择

时间:2014-02-20 08:40:06

标签: ios iphone ios7

禁用UITextview选择禁用或禁用复制/粘贴菜单,但我不想在UITextview中禁用我的链接 我尝试但两个都禁用任何解决方案?

5 个答案:

答案 0 :(得分:2)

如果您只想禁用复制/粘贴。你应该使用UITextView的子类

然后只需执行此操作即可禁用菜单中的所有项目:

- (BOOL)canBecomeFirstResponder {
    return NO;
}

如果您只想禁用复制和粘贴,则可以执行以下操作:

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

答案 1 :(得分:1)

in - textViewDidChangeSelection:委托方法,

textView.selectedRange = NSMakeRange(0,0);

答案 2 :(得分:1)

根据您的要求实施以下内容。它肯定会起作用:

- (BOOL)canPerformAction:(SEL)action withSender:(id)sender
{
  if (action == @selector(selectAll:))
   return NO;

  if (action == @selector(select:))
   return NO;

  if (action == @selector(cut:))
   return NO;

  if (action == @selector(copy:))
      return NO;

  if (action == @selector(paste:))
   return NO;

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

答案 3 :(得分:1)

你可以为UITextField过度- (BOOL)canPerformAction:(SEL)action withSender:(id)sender。见下面的代码 -

@interface MYLoginTextField : UITextField

@end

@implementation MYLoginTextField

- (BOOL)canPerformAction:(SEL)action withSender:(id)sender
{
    if (action == @selector(paste:) || action == @selector(copy:) || action == @selector(cut:) || action == @selector(select:) || action == @selector(selectAll:) || action == @selector(select:))
        return NO;

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

@end

答案 4 :(得分:-1)

试试这个

yourTextView.editable = NO