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:@"輸入手機號" 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) // 取消編輯
}