ViewController.m
@interface ViewController ()<UITextFieldDelegate>
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
UITextField * textField = [[UITextField alloc]initWithFrame:CGRectMake(100, 100, 100, 30)];
textField.delegate = self;
[textField setBackgroundColor:[UIColor redColor]];
[self.view addSubview:textField];
}
// YES: 能输入 NO:不能输入
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
return NO;
}
- (BOOL)canPerformAction:(SEL)action withSender:(nullable id)sender
{
return (action == @selector(paste:));
}
UITextField+Paste.m
@implementation UITextField (Paste)
- (void)paste:(UIMenuController *)menu
{
// 将粘贴板的文字赋值给label
self.text = [UIPasteboard generalPasteboard].string;
}