主要功能点:
1. textField关联键盘
2. 用户名和密码的限制和过滤
3. 键盘收回操作
Check on the code
#import "ViewController.h"
@interface ViewController()
@end
@implementation ViewController
-(void)viewDidLoad{
[super viewDidLoad];
UILabel *label1 = [[UILabel alloc]initWithFrame:CGRectMake(10, 50, 90, 20)];
label1.font = [UIFont systemFontOfSize:15];
label1.text = @"用 户 名:";
label1.backgroundColor = [UIColor clearColor];
label1.textAlignment = NSTextAlignmentLeft;
label1.numberOfLines = 2;//用于设置label中文本的行数
[self.view addSubview:label1];
UILabel *label2 = [[UILabel alloc]initWithFrame:CGRectMake(10, 90, 90, 20)];
label2.font = [UIFont systemFontOfSize:15];
label2.text = @"密 码:";
label2.backgroundColor = [UIColor clearColor];
label2.textAlignment = NSTextAlignmentLeft;
[self.view addSubview:label2];
UILabel *label3 = [[UILabel alloc]initWithFrame:CGRectMake(10, 130, 90, 20)];
label3.font = [UIFont systemFontOfSize:15];
label3.text = @"确认密码:";
label3.backgroundColor = [UIColor whiteColor];
label3.textAlignment = NSTextAlignmentLeft;
[self.view addSubview:label3];
UITextField *textField1 = [[UITextField alloc]initWithFrame:CGRectMake(110, 50, 200, 20)];
textField1.backgroundColor = [UIColor clearColor];
textField1.placeholder = @"请输入用户名";
textField1.tag = 1;
[textField1 setSecureTextEntry:NO];
textField1.font = [UIFont systemFontOfSize:14];
//textField1.delegate = self;
textField1.autocapitalizationType = UITextAutocapitalizationTypeNone;//否则默认首字母大写
textField1.borderStyle = UITextBorderStyleRoundedRect;
[self.view addSubview:textField1];
UITextField *psField1 = [[UITextField alloc]initWithFrame:CGRectMake(110, 90, 200, 20)];
psField1.backgroundColor = [UIColor clearColor];
psField1.placeholder = @"至少6位字母或数字";
psField1.tag = 2;
psField1.font = [UIFont systemFontOfSize:14];
//psField1.delegate = self;
psField1.borderStyle = UITextBorderStyleRoundedRect;
psField1.keyboardType = UIKeyboardTypeDefault;
psField1.keyboardAppearance = UIKeyboardAppearanceDark;
[psField1 setSecureTextEntry:YES];
[self.view addSubview:psField1];
UITextField *psField2 = [[UITextField alloc]initWithFrame:CGRectMake(110, 130, 200, 20)];
psField2.backgroundColor = [UIColor clearColor];
psField2.font = [UIFont systemFontOfSize:14];
psField2.borderStyle = UITextBorderStyleRoundedRect;
psField2.placeholder = @"请再次输入密码";
psField2.tag = 3;
psField2.keyboardType = UIKeyboardTypeDefault;
psField2.keyboardAppearance = UIKeyboardAppearanceDark;
[psField2 setSecureTextEntry:YES];
[self.view addSubview:psField2];
UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[btn setFrame:CGRectMake(110, 170, 180, 30)];
[btn setTitle:@"提交" forState:UIControlStateNormal];//正常状态下button显示的标题
[btn setTitle:@"提交" forState:UIControlStateHighlighted];//高亮显示时button的标题
btn.backgroundColor = [UIColor greenColor];
//button被按下又抬起后发生的事件
[btn addTarget:self action:@selector(confirm:) forControlEvents:UIControlEventTouchUpInside];
//selector可以理解为一个选择子,是指针变量,类似于sender。这里是将这个方法指定给新建的这个button
[self.view addSubview:btn];
}
//收回键盘
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
for (int i =0; i < 4; i++) {
UITextField *textField = (UITextField *)[self.view viewWithTag:1+i];
[textField resignFirstResponder];
}
}
}