效果如下:
ViewController.h
1 #import <UIKit/UIKit.h> 2 3 @interface ViewController : UIViewController<UITextFieldDelegate> 4 @property (strong, nonatomic) NSMutableArray *mArrTextField; 5 6 @end
ViewController.m
1 #import "ViewController.h" 2 3 @interface ViewController () 4 - (void)layoutUI; 5 - (UITextField *)newTextField:(UITextFieldViewMode)clearButtonMode frame:(CGRect)rect; 6 - (void)hideKeyboard:(id)sender; 7 @end 8 9 @implementation ViewController 10 - (void)viewDidLoad { 11 [super viewDidLoad]; 12 13 [self layoutUI]; 14 15 UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hideKeyboard:)]; 16 [self.view addGestureRecognizer:tapGestureRecognizer]; 17 } 18 19 - (void)didReceiveMemoryWarning { 20 [super didReceiveMemoryWarning]; 21 // Dispose of any resources that can be recreated. 22 } 23 24 #pragma mark - Private Methods 25 /** 26 * 隐藏键盘 27 * 28 * @param sender 事件触发者 29 */ 30 - (void)hideKeyboard:(id)sender { 31 NSLog(@"Tap Gesture Recognizer,去掉某些控件作为第一响应器"); 32 for (UITextField *txtF in _mArrTextField) { 33 [txtF resignFirstResponder]; 34 } 35 } 36 37 - (void)layoutUI { 38 _mArrTextField = [[NSMutableArray alloc] initWithCapacity:4]; 39 40 CGRect rectTemp = CGRectMake(20, 40, 320, 40); 41 UITextField *txtFTemp = [self newTextField:UITextFieldViewModeNever frame:rectTemp]; 42 [self.view addSubview:txtFTemp]; 43 [_mArrTextField addObject:txtFTemp]; 44 45 rectTemp.origin.y += 50; 46 txtFTemp = [self newTextField:UITextFieldViewModeWhileEditing frame:rectTemp]; 47 [self.view addSubview:txtFTemp]; 48 [_mArrTextField addObject:txtFTemp]; 49 50 rectTemp.origin.y += 50; 51 txtFTemp = [self newTextField:UITextFieldViewModeUnlessEditing frame:rectTemp]; 52 [self.view addSubview:txtFTemp]; 53 [_mArrTextField addObject:txtFTemp]; 54 55 rectTemp.origin.y += 50; 56 txtFTemp = [self newTextField:UITextFieldViewModeAlways frame:rectTemp]; 57 [self.view addSubview:txtFTemp]; 58 [_mArrTextField addObject:txtFTemp]; 59 } 60 61 - (UITextField *)newTextField:(UITextFieldViewMode)clearButtonMode frame:(CGRect)rect { 62 UITextField *txtFMessage = [[UITextField alloc] initWithFrame:rect]; 63 txtFMessage.textAlignment = NSTextAlignmentLeft; 64 txtFMessage.textColor = [UIColor colorWithRed:0.433 green:0.829 blue:1.000 alpha:1.000]; 65 txtFMessage.borderStyle = UITextBorderStyleRoundedRect; 66 67 txtFMessage.delegate = self; 68 txtFMessage.clearButtonMode = clearButtonMode; 69 switch (clearButtonMode) { 70 case UITextFieldViewModeNever: 71 txtFMessage.text = @"UITextFieldViewModeNever"; //不显示清空按钮;默认值 72 break; 73 case UITextFieldViewModeWhileEditing: 74 txtFMessage.text = @"UITextFieldViewModeWhileEditing"; //文本内容不为空时,且在编辑状态(即获得焦点)时显示清空按钮 75 break; 76 case UITextFieldViewModeUnlessEditing: 77 txtFMessage.text = @"UITextFieldViewModeUnlessEditing"; //文本内容不为空时,且不在编辑状态(即失去焦点)时显示清空按钮 78 break; 79 case UITextFieldViewModeAlways: 80 txtFMessage.text = @"UITextFieldViewModeAlways"; //文本内容不为空时,获得焦点与失去焦点都显示清空按钮 81 break; 82 } 83 return txtFMessage; 84 } 85 86 #pragma mark - TextField 87 - (BOOL)textFieldShouldClear:(UITextField *)textField { 88 NSLog(@"textFieldShouldClear: %@", textField.text); //记录清空按钮触发清空的文本内容;例如:UITextFieldViewModeWhileEditing 89 return YES; 90 } 91 92 @end
时间: 2024-10-10 15:58:35