#pragma mark - 加载视图
- (void)viewDidLoad {
[super viewDidLoad];
// @interface ViewController : UIViewController<UITextFieldDelegate>
//UITextField常用的输入控件,输用户名或者密码
//是否显示密码,是否显示提示信息,是否显示清除按钮
CGRect frame = CGRectMake(20, 20, 150, 40);
self.textFiel = [[UITextField alloc] initWithFrame:frame];
self.textFiel.delegate = self;
//提示信息
self.textFiel.placeholder = @"这是一个提示信息";
//边框样式
self.textFiel.borderStyle = UITextBorderStyleRoundedRect;
//内容垂直方向对齐
self.textFiel.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
//内容水平方向对齐
self.textFiel.textAlignment = NSTextAlignmentLeft;
//密码
self.textFiel.secureTextEntry = YES;
//是否自动验证输入内容
self.textFiel.autocorrectionType = UITextAutocorrectionTypeNo;
//单词首字母大小写
self.textFiel.autocapitalizationType = UITextAutocapitalizationTypeNone;
//键盘返回类型
self.textFiel.returnKeyType = UIReturnKeyDone;
//编辑时的清除按钮
self.textFiel.clearButtonMode = UITextFieldViewModeWhileEditing;
[self.view addSubview:self.textFiel];
}
#pragma mark - 做todo事件
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark -
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
NSLog(@"textFieldShouldBeginEditing");
return YES;
}
- (void)textFieldDidBeginEditing:(UITextField *)textField{
NSLog(@"textFieldDidBeginEditing");
}
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField{
NSLog(@"textFieldShouldEndEditing");
return YES;
}
- (void)textFieldDidEndEditing:(UITextField *)textField{
NSLog(@"textFieldDidEndEditing");
}
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
NSLog(@"shouldChangeCharactersInRange");
return YES;
}
- (BOOL)textFieldShouldClear:(UITextField *)textField{
NSLog(@"textFieldShouldClear");
return YES;
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
//隐藏键盘
[self.textFiel resignFirstResponder];
NSLog(@"textFieldShouldReturn");
return YES;
}
#pragma mark - 点击next或者done事件
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
if (textField == _inputCardNumber) {
[_inputBirthDay becomeFirstResponder];
}else if(textField == _inputBirthDay){
[_inputIDNumber becomeFirstResponder];
}else if(textField == _inputIDNumber){
[_inputCardCheckNumber becomeFirstResponder];
}else if(textField==_inputCardCheckNumber){
[_inputPhoneNumber becomeFirstResponder];
}else if(textField == _inputPhoneNumber){
[_inputCardNumber resignFirstResponder];
[_inputPhoneNumber resignFirstResponder];
[_inputIDNumber resignFirstResponder];
[_inputBirthDay resignFirstResponder];
[_inputCardCheckNumber resignFirstResponder];
return YES;
}
[self scrolleInputViewToFitKeyboard:kbHeight animationDuration:keyboardAnimationDuration];
return YES;
}
#pragma mark - keyboard events
- (void) keyboardWillShow:(NSNotification *)notification {
//获取键盘高度,在不同设备上,以及中英文下是不同的
kbHeight = [[notification.userInfo objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size.height;
// 取得键盘的动画时间,这样可以在视图上移的时候更连贯
keyboardAnimationDuration = [[notification.userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];
[self scrolleInputViewToFitKeyboard:kbHeight animationDuration:keyboardAnimationDuration];
}
#pragma mark - 滚动屏幕
- (void) scrolleInputViewToFitKeyboard:(CGFloat)keyHeight animationDuration: (double) duration{
//计算出键盘顶端到底端的距离
CGFloat inputViewContainerPositionY = _inputViewContainer.frame.origin.y;
CGFloat responsPositionY = responsView.frame.origin.y;
CGFloat responsPositionHeight = responsView.frame.size.height;
CGFloat viewHeight = self.view.frame.size.height;
CGFloat offset = inputViewContainerPositionY + responsPositionY + responsPositionHeight/2 - (viewHeight - kbHeight);
//将视图上移计算好的偏移
if(offset > 0) {
[UIView animateWithDuration:duration animations:^{
self.view.frame = CGRectMake(0.0f, -offset, self.view.frame.size.width, self.view.frame.size.height);
}];
}
}
1.通过XIB方式实现:
将UITextField中的secure选项勾中即可。
storyboard 勾选 Secure Text Entry即可
2.通过代码实现:
UItextField * test = [ UItextField alloc] init ];
test.secureTextEntry = YES;
以上两种方式实现文本框的密码样式。
#pragma mark - 文本框改变内容 -
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
NSLog(@"%d",range.location);//0代表有一位数 1是两位数,代表光标位置
NSLog(@"%d",range.length);//长度 0 为增加一位数 1为删除一位数
NSLog(@"%@",string);
textField.text = string;//设置文字
if (textField == _textfieldPWD0) {
if(range.length == 0){//增加 移到下一位
[_textfieldPWD1 becomeFirstResponder];//获得第一响应焦点
}
return NO;
}else if(textField == _textfieldPWD5){
if (range.length == 0) {
[self resignPWDFirstResponder];
}else{
[_textfieldPWD4 becomeFirstResponder];
}
return NO;
}
int length = [textfiledPWDs count];
for (int i = 1; i < length - 1; i++) {
if (textField == [textfiledPWDs objectAtIndex:i]) {
if (range.length == 0) {
[[textfiledPWDs objectAtIndex:i+1] becomeFirstResponder];
}else{
[[textfiledPWDs objectAtIndex:i-1] becomeFirstResponder];
}
return NO;
}
}
//返回 NO 此次编辑的文字不会被写到第一响应的文本域里面
return NO;
}