UITextField 屬性.png
_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:@"輸入手機(jī)號(hào)" attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:23],NSForegroundColorAttributeName:[UIColor colorFromHexRGB:@"20abff"]}];
_phoneTextField.defaultTextAttributes = @{NSFontAttributeName:[UIFont systemFontOfSize:23],NSForegroundColorAttributeName:[UIColor blackColor]}; // 正常狀態(tài)下的字體顏色
_phoneTextField.tintColor = [UIColor lightGrayColor]; // 光標(biāo)占位符的顏色
_phoneTextField.attributedPlaceholder = attribute; // Placeholder 的屬性 (字體大小, 字體顏色)
協(xié)議方法
// 是否允許開(kāi)始編輯
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
return true;
}
// 開(kāi)始編輯時(shí)調(diào)用(成為第一響應(yīng)者)
// became first responder
- (void)textFieldDidBeginEditing:(UITextField *)textField{
}
// 是否允許結(jié)束編輯
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField{
return true;
}
// 當(dāng)結(jié)束編輯時(shí)調(diào)用
- (void)textFieldDidEndEditing:(UITextField *)textField{
}
// 替換textFieldDidEndEditing 【UITextFieldDidEndEditingReason見(jiàn)下】
- (void)textFieldDidEndEditing:(UITextField *)textField reason:(UITextFieldDidEndEditingReason)reason NS_AVAILABLE_IOS(10_0){
}
// 是否允許文本框的內(nèi)容(no/false 攔截用書(shū)輸入 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) // 取消編輯
}