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? I.e 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 wha开发者_运维问答t 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
Without subclassing, but also without auto-selection, i.e 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;
}
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:
- Put
"UITextFieldDelegate"
in angle brackets after ViewController declaration in @interface In the
-(void)viewDidLoad{}
method add the following two lines:self.outputTextField.delegate = self; self.outputTextField.inputView = [[UIView alloc] init];
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
精彩评论