Storyboard学习五(简单注册页面实现)

主要功能点:
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];
    }
}

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值