简体   繁体   中英

Non-editable UITextField with copy menu

Is there a way to make a UITextField non-editable by the user and provide only a copy menu without subclassing? Ie when the text field is touched it automatically selects all the text and only shows the copy menu.

If possible to do this without subclassing what are the interface builder options that need to be selected?

Thanks in advance.

simply do:

(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
    return NO;
}

and also:

yourTextField.inputView = [[UIView alloc] init];

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

Items required to make a UITextField (in this case, outputTextField) able to accept touch and allow Select, Select All, Copy, Paste, but NOT edit the area and also DISABLE popup keyboard:

  1. Put "UITextFieldDelegate" in angle brackets after ViewController declaration in @interface
  2. In the -(void)viewDidLoad{} method add the following two lines:

     self.outputTextField.delegate = self; self.outputTextField.inputView = [[UIView alloc] init];
  3. Add delegate method (see actual method below):

     textField: shouldChangeCharactersInRange:replacementString: { }
 // ViewController.h #import <UIKit/UIKit.h> @interface ViewController: UIViewController @end // ViewController.m #import "ViewController.h" @interface ViewController () <UITextFieldDelegate> @property (weak, nonatomic) IBOutlet UITextField *inputTextField; @property (weak, nonatomic) IBOutlet UITextField *outputTextField; - (IBAction)copyButton:(UIButton *)sender; @end @implementation ViewController - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range

replacementString:(NSString *)string
{

 NSLog(@"inside shouldChangeCharactersInRange"); if (textField == _outputTextField) { [_outputTextField resignFirstResponder]; } return NO; } - (void)viewDidLoad { [super viewDidLoad]; self.outputTextField.delegate = self; // delegate methods won't be called without this self.outputTextField.inputView = [[UIView alloc] init]; // UIView replaces keyboard } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; } - (IBAction)copyButton:(UIButton *)sender { self.outputTextField.text = self.inputTextField.text; [_inputTextField resignFirstResponder]; [_outputTextField resignFirstResponder]; } @end

This worked for me to have UITextField non Editable and only allow copy. Just change your UITextField Class to CopyAbleTF from XIB and you are set.

@implementation CopyAbleTF

- (void)awakeFromNib{


    self.inputView= [[UIView alloc] init];

}

- (BOOL)canPerformAction:(SEL)action withSender:(id)sender
{

    if (action == @selector(copy:))
    {
        return true;
    }

    return false;
}



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



@end

Without subclassing, but also without auto-selection, ie the user can select, keyboard shows up, but the user cannot input data:

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
    return NO;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM