简单的TextField输入框布局。默认键盘不显示,点击输入框键盘出现,点击return和空白区域键盘隐藏。
密码隐藏功能,点击右边按钮密码显示并切换图片。
监听输入,当用户名和密码都输入了,且密码超过6位,提交按钮背景颜色改变。
//
// ChangePasswordViewController.m
// NetsUi
//
// Created by lishuai on 2017/8/1.
// Copyright © 2017年 lishuai. All rights reserved.
//
#import "ChangePasswordViewController.h"
@interface ChangePasswordViewController ()<UITableViewDelegate,UITableViewDataSource,UITextFieldDelegate>{
UITextField* user;
UITextField* pass;
UIButton *toggleB;
}
@property(nonatomic,strong)UITableView* tableView;
@end
@implementation ChangePasswordViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor colorWithRed:243.0/255 green:243.0/255 blue:243.0/255 alpha:1];
[self confirmButton];
user.delegate = self;
pass.delegate = self;
//隐藏键盘
[user resignFirstResponder];
[pass resignFirstResponder];
}
//回车隐藏键盘
-(BOOL)textFieldShouldReturn:(UITextField *)textField{
[textField resignFirstResponder];
return YES;
}
//触摸别的地方隐藏键盘
-(void)touchesBegan:(NSSet*)touches withEvent:(UIEvent *)event{
[super touchesBegan:touches withEvent:event];
[user resignFirstResponder];
[pass resignFirstResponder];
}
-(void)confirmButton{
user = [[UITextField alloc]initWithFrame:CGRectMake(0, 60, self.view.frame.size.width, 54) ];
user.tag=1;
user.backgroundColor = [UIColor whiteColor];
user.placeholder = @"Old Password";
user.leftView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 8, user.frame.size.height) ];
user.leftViewMode = UITextFieldViewModeAlways;
user.secureTextEntry = YES;
user.layer.borderColor = [UIColor colorWithRed:230.0/255 green:230.0/255 blue:230.0/255 alpha:1].CGColor;
user.layer.borderWidth = 1.0;
//textField.keyboardType = UIKeyboardTypeASCIICapable;
[user setBorderStyle:UITextBorderStyleLine];
//[textField setTextAlignment:UITextAlignmentCenter];
[user setKeyboardType:UIKeyboardTypeNumbersAndPunctuation];
[user becomeFirstResponder];
[self.view addSubview:user];
pass = [[UITextField alloc]initWithFrame:CGRectMake(0, 160, self.view.frame.size.width, 54) ];
pass.tag=2;
pass.backgroundColor = [UIColor whiteColor];
pass.placeholder = @"New Password(6-12 letters and numbers)";
pass.leftView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 8, pass.frame.size.height) ];
pass.leftViewMode = UITextFieldViewModeAlways;
pass.secureTextEntry = YES;
toggleB = [UIButton buttonWithType:UIButtonTypeCustom];
[toggleB setFrame:CGRectMake(2, 2, 22, 22)];
[toggleB setBackgroundImage:[UIImage imageNamed:@"icon_app_select"] forState:UIControlStateNormal];
[toggleB setBackgroundImage:[UIImage imageNamed:@"icon_app_unselect"] forState:UIControlStateSelected];
[toggleB addTarget:self action:@selector(buttontoogle:) forControlEvents:UIControlEventTouchUpInside];
UIView *bottomView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 30, 30)];
[bottomView addSubview:toggleB];
pass.rightView = bottomView;
pass.rightViewMode = UITextFieldViewModeAlways;
pass.layer.borderColor = [UIColor colorWithRed:230.0/255 green:230.0/255 blue:230.0/255 alpha:1].CGColor;
pass.layer.borderWidth = 1.0;
[pass setBorderStyle:UITextBorderStyleLine];
[pass setKeyboardType:UIKeyboardTypeNumbersAndPunctuation];
[pass becomeFirstResponder];
[self.view addSubview:pass];
UIButton* conButton = [[UIButton alloc]initWithFrame:CGRectMake(20, 260, self.view.frame.size.width-40, 54)];
conButton.tag=3;
[conButton setTitle:@"confirm" forState:UIControlStateNormal];
[conButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
conButton.contentEdgeInsets = UIEdgeInsetsMake(0, 10, 0, 0);
conButton.backgroundColor = [UIColor grayColor];
conButton.layer.cornerRadius = 4.0;
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(confirm:) name:UITextFieldTextDidChangeNotification object:nil];
[self.view addSubview:conButton];
}
-(void)buttontoogle:(UIButton*)sender{
sender.selected = !sender.selected;
//NSLog(@"%i",toggleB.selected);
if(!toggleB.selected){
pass.secureTextEntry = YES;
}else{
pass.secureTextEntry = NO;
}
}
-(void)confirm:(NSNotification*)notification
{
UITextField *field1=(UITextField*)[self.view viewWithTag:1];
UITextField *field2=(UITextField*)[self.view viewWithTag:2];
if(field1.text.length>0 &&field2.text.length>5){
[self.view viewWithTag:3].backgroundColor = [UIColor colorWithRed:0/255 green:125.0/255 blue:207.0/255 alpha:1];
}else{
[self.view viewWithTag:3].backgroundColor = [UIColor colorWithRed:188.0/255 green:188.0/255 blue:188.0/255 alpha:1];
}
};
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end