Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How disable Copy, Cut, Select, Select All in UITextView

The UITextView's Copy, Cut, Select, Select All functionality is shown by default when I press down on the screen. But, in my project the UITextField is only read only. I do not require this functionality. Please tell me how to disable this feature.

like image 635
Aishwarya Avatar asked Sep 15 '09 12:09

Aishwarya


7 Answers

The easiest way to disable pasteboard operations is to create a subclass of UITextView that 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;
    return [super canPerformAction:action withSender:sender];
}

Also see UIResponder

like image 64
rpetrich Avatar answered Oct 23 '22 16:10

rpetrich


Subclass UITextView and overwrite canBecomeFirstResponder:

- (BOOL)canBecomeFirstResponder {
    return NO;
}

Note, that this only applies for non-editable UITextViews! Haven't tested it on editable ones...

like image 45
iCoder Avatar answered Oct 23 '22 15:10

iCoder


This was the best working solution for me:

UIView *overlay = [[UIView alloc] init];  
[overlay setFrame:CGRectMake(0, 0, myTextView.contentSize.width, myTextView.contentSize.height)];  
[myTextView addSubview:overlay];  
[overlay release];

from: https://stackoverflow.com/a/5704584/1293949

like image 44
Saraiva Alcides Avatar answered Oct 23 '22 16:10

Saraiva Alcides


If you want to disable cut/copy/paste on all UITextView of your application you can use a category with :

@implementation UITextView (DisableCopyPaste)

- (BOOL)canBecomeFirstResponder
{
    return NO;
}

@end

It saves a subclassing... :-)

like image 38
Damien Debin Avatar answered Oct 23 '22 16:10

Damien Debin


@rpetrich answer worked for me. I'm posting the expanded code in case it saves someone some time.

In my case I want no popup whatsoever, but I do want the UITextField to be able to become first responder.

Unfortunately, you still get the magnifier popup when you tap and hold the textfield.

@interface NoSelectTextField : UITextField

@end

@implementation NoSelectTextField

- (BOOL)canPerformAction:(SEL)action withSender:(id)sender {
    if (action == @selector(paste:) ||
        action == @selector(cut:) ||
        action == @selector(copy:) ||
        action == @selector(select:) ||
        action == @selector(selectAll:) ||
        action == @selector(delete:) ||
        action == @selector(makeTextWritingDirectionLeftToRight:) ||
        action == @selector(makeTextWritingDirectionRightToLeft:) ||
        action == @selector(toggleBoldface:) ||
        action == @selector(toggleItalics:) ||
        action == @selector(toggleUnderline:)
        ) {
            return NO;
    }
    return [super canPerformAction:action withSender:sender];
}

@end

Swift 4

class NoSelectTextField: UITextField {

    override func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool {
        if action == #selector(paste(_:)) ||
            action == #selector(cut(_:)) ||
            action == #selector(copy(_:)) ||
            action == #selector(select(_:)) ||
            action == #selector(selectAll(_:)) ||
            action == #selector(delete(_:)) ||
            action == #selector(makeTextWritingDirectionLeftToRight(_:)) ||
            action == #selector(makeTextWritingDirectionRightToLeft(_:)) ||
            action == #selector(toggleBoldface(_:)) ||
            action == #selector(toggleItalics(_:)) ||
            action == #selector(toggleUnderline(_:)) {
            return false
        }
        return super.canPerformAction(action, withSender: sender)
    }

}
like image 26
Devin Pitcher Avatar answered Oct 23 '22 14:10

Devin Pitcher


If you don't need UITextView to scroll, then the simplest solution that doesn't involve sub-classing is to simply disable user interaction for the text view:

textField.userInteractionEnabled = NO;
like image 20
Luke Redpath Avatar answered Oct 23 '22 16:10

Luke Redpath


The easiest way is to create a subclass of UITextView that overrides the canPerformAction:withSender:

- (BOOL)canPerformAction:(SEL)action withSender:(id)sender    
{    
     [UIMenuController sharedMenuController].menuVisible = NO;  //do not display the menu
     [self resignFirstResponder];                      //do not allow the user to selected anything
     return NO;
}
like image 15
haiLong Avatar answered Oct 23 '22 15:10

haiLong