_phoneTextField = [[UITextField alloc]init];
_phoneTextField.borderStyle = UITextBorderStyleRoundedRect; //圆角边框
_phoneTextField.layer.cornerRadius = 37.5; // 切圆角
_phoneTextField.clipsToBounds = true;
_phoneTextField.layer.borderColor = [[UIColor colorFromHexRGB:@"20abff"] CGColor]; // 边框颜色
_phoneTextField.layer.borderWidth = 1.0;
NSMutableAttributedString * attribute = [[NSMutableAttributedString alloc]initWithString:@"输入手机号" attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:23],NSForegroundColorAttributeName:[UIColor colorFromHexRGB:@"20abff"]}];
_phoneTextField.defaultTextAttributes = @{NSFontAttributeName:[UIFont systemFontOfSize:23],NSForegroundColorAttributeName:[UIColor blackColor]}; // 正常状态下的字体颜色
_phoneTextField.tintColor = [UIColor lightGrayColor]; // 光标占位符的颜色
_phoneTextField.attributedPlaceholder = attribute; // Placeholder 的属性 (字体大小, 字体颜色)
协议方法
// 是否允许开始编辑
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
return true;
}
// 开始编辑时调用(成为第一响应者)
// became first responder
- (void)textFieldDidBeginEditing:(UITextField *)textField{
}
// 是否允许结束编辑
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField{
return true;
}
// 当结束编辑时调用
- (void)textFieldDidEndEditing:(UITextField *)textField{
}
// 替换textFieldDidEndEditing 【UITextFieldDidEndEditingReason见下】
- (void)textFieldDidEndEditing:(UITextField *)textField reason:(UITextFieldDidEndEditingReason)reason NS_AVAILABLE_IOS(10_0){
}
// 是否允许文本框的内容(no/false 拦截用书输入 true/yes 允许用户输入)
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
return false;
}
typedef NS_ENUM(NSInteger, UITextFieldDidEndEditingReason) {
UITextFieldDidEndEditingReasonCommitted, // 提交编辑
UITextFieldDidEndEditingReasonCancelled UIKIT_AVAILABLE_TVOS_ONLY(10_0) // 取消编辑
}
网友评论