基本属性
//UITextField, 单行文本输入框, 继承与UIControl
UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(20, 300, 370 - 40, 40)];
//视图背景颜色
textField.backgroundColor = [UIColor blackColor];
//属性
//占位符(用于提示输入)
textField.placeholder = @"请输入您的密码";//用作提示输入
//文本(预先输入的值)
textField.text = @"";
//边框样式, 默认: none(没有)
textField.borderStyle = UITextBorderStyleRoundedRect;
//文字颜色
textField.textColor = [UIColor purpleColor];
//设置占位文字颜色
[textField setValue:[UIColor yellowColor] forKeyPath:@"_placeholderLabel.textColor"];
//设置光标的颜色
textField.tintColor = [UIColor whiteColor];
//字体
textField.font = [UIFont systemFontOfSize:30];
//是否安全输入(默认为: NO 关闭安全输入)
textField.secureTextEntry = YES;
//重新开始编辑时, 是否清除内容默认: NO(不清除)
textField.clearsOnBeginEditing = YES;
//清除按钮的显示模式 :默认 不显示
//UITextFieldViewModeNever: 不显示
//UITextFieldViewModeWhileEditing: 编辑时显示
//UITextFieldViewModeUnlessEditing: 不编辑时显示
//UITextFieldViewModeAlways: 一直显示(前提是文本框有内容, 没内容时显示的是预输入值)
textField.clearButtonMode = UITextFieldViewModeAlways;//一直显示
UIButton *clearButton = [textField valueForKey:@"_clearButton"];
[clearButton setImage:[UIImage imageNamed:@"清除"] forState:UIControlStateNormal];//自定义清除按钮
//键盘return键样式
textField.returnKeyType = UIReturnKeyNext;
//设定代理
textField.delegate = self;
//键盘样式
textField.keyboardType = UIKeyboardTypeDefault;
//添加方法(文本发生改变时调用)
[self.simKaHao addTarget:self action:@selector(updataSimNumber:) forControlEvents:(UIControlEventEditingChanged)];
[self.view addSubview:textField];
UITextFieldDelegate
#pragma mark ------> UITextFieldDelegate
/**
是否允许编辑
@return YES:允许编辑, NO:不允许编辑
*/
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
return YES;
}
/**
成为第一响应者时的代理回调(开始编辑时)
*/
- (void)textFieldDidBeginEditing:(UITextField *)textField {
NSLog(@"已经开始编辑");
}
/**
将要结束编辑
@return YES:允许结束编辑, NO:不允许结束编辑
*/
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField {
return YES;
}
/**
已经结束编辑(iOS10及以上可用)
*/
- (void)textFieldDidEndEditing:(UITextField *)textField {
NSLog(@"已经结束编辑老");
}
/**
已经结束编辑(iOS10及以上可用, 如果此方法被调用,则textFieldDidEndEditing:方法就不被调用)
*/
- (void)textFieldDidEndEditing:(UITextField *)textField reason:(UITextFieldDidEndEditingReason)reason NS_AVAILABLE_IOS(10_0) {
NSLog(@"已经结束编辑新");
}
/**
文本将要更改时调用
@param range 范围
@param string 将要更改的字符串
@return YES:更改文本, NO:不更改文本
*/
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
return YES;
}
/**
点击清除按钮时调用(执行清除方法时调用)
@return YES:允许清除, NO:不允许清除
*/
- (BOOL)textFieldShouldClear:(UITextField *)textField {
NSLog(@"点击了清除按钮");
return YES;
}
/**
点击return按钮时调用
@return 如果执行默认行为则返回YES, 否则返回NO(暂时木有没有发现有啥用)
*/
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
NSLog(@"点击返回按钮");
[textField resignFirstResponder];
return NO;
}
return样式
- UIReturnKeyDefault, //默认:灰色按钮,标有Return
- UIReturnKeyGo, //标有Go的蓝色按钮
- UIReturnKeyGoogle, //标有Google的蓝色按钮,用于搜索
- UIReturnKeyJoin, //标有Join的蓝色按钮
- UIReturnKeyNext, //标有Next的蓝色按钮
- UIReturnKeyRoute, //标有Route的蓝色按钮
- UIReturnKeySearch, //标有Search的蓝色按钮
- UIReturnKeySend, //标有Send的蓝色按钮
- UIReturnKeyYahoo, //标有Yahoo!的蓝色按钮,用于搜索
- UIReturnKeyDone, //标有Done的蓝色按钮
- UIReturnKeyEmergencyCall, //紧急呼叫按钮
键盘样式:
-
UIKeyboardTypeASCIICapable和UIKeyboardTypeAlphabet
UIKeyboardTypeASCIICapable.png -
UIKeyboardTypeNumbersAndPunctuation
UIKeyboardTypeNumbersAndPunctuation.png -
UIKeyboardTypeURL
UIKeyboardTypeURL.png -
UIKeyboardTypeNumberPad
UIKeyboardTypeNumberPad.png
-
UIKeyboardTypePhonePad
UIKeyboardTypePhonePad.png -
UIKeyboardTypeNamePhonePad
UIKeyboardTypeNamePhonePad.png
UIKeyboardTypeNamePhonePad123
UIKeyboardTypeNamePhonePad123.png
-
UIKeyboardTypeEmailAddress
UIKeyboardTypeEmailAddress.png
UIKeyboardTypeEmailAddress123
UIKeyboardTypeEmailAddress123.png -
UIKeyboardTypeDecimalPad
UIKeyboardTypeDecimalPad.png -
UIKeyboardTypeTwitter
UIKeyboardTypeTwitter.png
UIKeyboardTypeTwitter123
UIKeyboardTypeTwitter123.png -
UIKeyboardTypeWebSearch
UIKeyboardTypeWebSearch.png
UIKeyboardTypeWebSearch123
UIKeyboardTypeWebSearch123.png -
UIKeyboardTypeASCIICapableNumberPad
UIKeyboardTypeASCIICapableNumberPad.png
网友评论