关于UITextField的简单实用和注释 1 - (void)viewDidLoad { 2 [super viewDidLoad]; 3 4 // self.view.backgroundColor = [UIColor grayColor]; 5 6 //输入框默认为全透明 7 //不管frame多大,默认输入框高度为30 8 UITextField *textfield1 = [[UITextField alloc] initWithFrame:CGRectMake(100, 100, 200, 30)]; 9 //设置边框类型,默认为无边框 10 textfield1.borderStyle = UITextBorderStyleLine; 11 //设置背景颜色,默认为透明色 12 textfield1.backgroundColor = [UIColor orangeColor]; 13 14 [self.view addSubview:textfield1]; 15 16 17 //设置居中 18 textfield1.textAlignment = NSTextAlignmentCenter; 19 //设置密码显示形式 20 // textfield1.secureTextEntry = YES; 21 22 //设置键盘类型 23 textfield1.keyboardType = UIKeyboardTypeEmailAddress; 24 25 //设置清除按钮合适显示 26 textfield1.clearButtonMode = UITextFieldViewModeWhileEditing; 27 28 //关闭首字母大写 29 textfield1.autocapitalizationType = UITextAutocapitalizationTypeNone; 30 //输入框为空时的提示文字 31 textfield1.placeholder = @"请输入您的帐号"; 32 33 textfield1.text = @"hahha"; 34 //设置文本字体 35 textfield1.font = [UIFont systemFontOfSize:11]; 36 //设置代理 37 textfield1.delegate = self; 38 // self.ibTextField.delegate = self; 39 40 textfield1.tag = 100; 41 // self.ibTextField.tag = 200; 42 /* 43 使用代理方法: 44 1.遵从协议 45 2.设置代理 46 3.实现协议方法 47 48 */ 49 50 51 } 52 53 #define mark textFeildDelegate 54 //开始编辑 55 - (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{ 56 57 NSLog(@"j将要开始编辑,%ld",textField.tag); 58 //决定你的textField是否可以编辑 59 return YES; 60 } 61 - (void)textFieldDidBeginEditing:(UITextField *)textField{ 62 NSLog(@"开始编辑"); 63 64 } 65 - (BOOL)textFieldShouldEndEditing:(UITextField *)textField{ 66 67 NSLog(@"将要结束编辑"); 68 return YES; 69 70 } 71 72 - (void)textFieldDidEndEditing:(UITextField *)textField{ 73 74 NSLog(@"结束编辑"); 75 } 76 //点击清除按钮的时候调用 77 - (BOOL)textFieldShouldClear:(UITextField *)textField{ 78 NSLog(@"clear"); 79 return YES; 80 } 81 //点击键盘return时调用 82 - (BOOL)textFieldShouldReturn:(UITextField *)textField{ 83 NSLog(@"return"); 84 //取消第一响应者,收起键盘 85 [textField resignFirstResponder]; 86 return YES; 87 }
时间: 2024-11-05 19:45:01