Enable Copy and Paste on Uitextfield Without Making It Editable

Enable copy and paste on UITextField without making it editable

My final solution was the following:

I created a subclass of UILabel (UITextField should work the same) that displays a UIMenuController after being tapped. CopyableLabel.m looks like this:

@implementation CopyableLabel


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


- (BOOL)canBecomeFirstResponder {
return YES;
}


- (BOOL)becomeFirstResponder {
if([super becomeFirstResponder]) {
self.highlighted = YES;
return YES;
}
return NO;
}


- (void)copy:(id)sender {
UIPasteboard *board = [UIPasteboard generalPasteboard];
[board setString:self.text];
self.highlighted = NO;
[self resignFirstResponder];
}


- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
if([self isFirstResponder]) {
self.highlighted = NO;
UIMenuController *menu = [UIMenuController sharedMenuController];
[menu setMenuVisible:NO animated:YES];
[menu update];
[self resignFirstResponder];
}
else if([self becomeFirstResponder]) {
UIMenuController *menu = [UIMenuController sharedMenuController];
[menu setTargetRect:self.bounds inView:self];
[menu setMenuVisible:YES animated:YES];
}
}


@end

Non-editable UITextField with copy menu

You can disable editing by setting the enabled property of UITextField to NO.

textField.enabled = NO;

Note that doing this will also disable the copy/paste options.
What you are asking for has been discussed before in this solution: Enable copy and paste on UITextField without making it editable

How to disable pasting in a TextField in Swift?

I agree with Leonardo Savio Dabus, if I were you I'd use string checking and just give out a warning, it makes things easier. BUT, if disabling paste option is a fancy feature you really want to put into your app, then you need to do more work. I'll provide the steps below.

Step 1: You need to create another class which extends the UITextField. In this example, I made my CustomUITextField.

import Foundation
import UIKit //Don't forget this

class CustomUITextField: UITextField {
override func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool {
if action == #selector(UIResponderStandardEditActions.paste(_:)) {
return false
}
return super.canPerformAction(action, withSender: sender)
}
}

Step 2: Wire the storyboard with your ViewController. You need to declare an IBOutlet as in normal case:

@IBOutlet var textFieldA: CustomUITextField?

Wire the circle next to the @IBOutlet to the TextField in the storyboard. THEN, this is important and easy to be ignored:

  • Go to your storyboard
  • Click the target TextField
  • Select Identity Inspector (the 3rd one)
  • Change the class to CustomUITextField

Quick snapshot is provided below.

Sample Image

That's it, hope this works.

Credit:

Main reference

If you want to know more about the behavior of canPerformAction method, though it's an Objective-C version, the concepts are shared here.

How to disable copy/paste option in UITextfield in ios7

I think this method is ok,since no making of category etc.It works fine for me.

    [[NSOperationQueue mainQueue] addOperationWithBlock:^{
[[UIMenuController sharedMenuController] setMenuVisible:NO animated:NO];
}];
return [super canPerformAction:action withSender:sender];

Restricting copy,paste option for a particular UITextfield

Create a subclass of UITextField. In that subclass, implement


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

Then use this subclass for the field that you don't want to be able to copy in, and use a regular UITextField for the one that you can copy from.



Related Topics



Leave a reply



Submit