UITextField的基本使用
- 有時(shí)候光標(biāo)距離textField的左邊太近 想調(diào)節(jié)一下距離:
// 設(shè)置文本框左邊的內(nèi)容
UIView *leftView = [[UIView alloc] init];
leftView.frame = CGRectMake(0, 0, 10, 0);
textField.leftView = leftView;
//模式設(shè)置為一直顯示
textField.leftViewMode = UITextFieldViewModeAlways;
-
常見代理方法
//是否允許開始編輯 - (BOOL)textFieldShouldBeginEditing:(UITextField *)textField; //是否允許結(jié)束編輯 - (BOOL)textFieldShouldEndEditing:(UITextField *)textField; // 是否允許用戶輸入文字 - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string; // 文本框開始編輯的時(shí)候調(diào)用 - (void)textFieldDidBeginEditing:(UITextField *)textField;
鍵盤彈出時(shí)的notification
彈出的通知名稱
鍵盤狀態(tài)改變的時(shí)候,系統(tǒng)會(huì)發(fā)出一些特定的通知
UIKeyboardWillShowNotification // 鍵盤即將顯示
UIKeyboardDidShowNotification // 鍵盤顯示完畢
UIKeyboardWillHideNotification // 鍵盤即將隱藏
UIKeyboardDidHideNotification // 鍵盤隱藏完畢
UIKeyboardWillChangeFrameNotification//鍵盤的位置尺寸即將發(fā)生改變
UIKeyboardDidChangeFrameNotification // 鍵盤的位置尺寸改變完畢
- 通知中包含的有用的信息
系統(tǒng)發(fā)出鍵盤通知時(shí),會(huì)附帶一下跟鍵盤有關(guān)的額外信息(字典),字典常見的key如下:
UIKeyboardFrameBeginUserInfoKey // 鍵盤剛開始的frame
UIKeyboardFrameEndUserInfoKey // 鍵盤最終的frame(動(dòng)畫執(zhí)行完畢后)
UIKeyboardAnimationDurationUserInfoKey // 鍵盤動(dòng)畫的時(shí)間
UIKeyboardAnimationCurveUserInfoKey // 鍵盤動(dòng)畫的執(zhí)行節(jié)奏(快慢)
鍵盤彈出和消失的時(shí)候屏幕的改變
- 在鍵盤彈出和消失的時(shí)候一般控制器的view會(huì)作出相應(yīng)的改變,以便鍵盤不會(huì)遮擋住view
- (void)viewDidLoad {
[super viewDidLoad];
// 監(jiān)聽鍵盤通知
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillChangeFrame:)
name:UIKeyboardWillChangeFrameNotification
object:nil];
}
- (void)keyboardWillChangeFrame:(NSNotification *)notification {
// 取出鍵盤最終的frame
CGRect rect = [notification.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
// 取出鍵盤彈出需要花費(fèi)的時(shí)間
double duration = [notification.userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue];
// 修改transform
[UIView animateWithDuration:duration animations:^{
CGFloat ty = [UIScreen mainScreen].bounds.size.height - rect.origin.y;
self.view.transform = CGAffineTransformMakeTranslation(0, - ty);
}];
}
UITextField的編輯事件的監(jiān)聽
- 通過UIControl的addTarget方法
[textField addTarget:target action:@selector(editingDidBegin) forControlEvents:UIControlEventEditingDidBegin];
[textField addTarget:target action:@selector(editingDidEnd) forControlEvents:UIControlEventEditingDidEnd];
- 通過代理
- (void)textFieldDidBeginEditing:(UITextField *)textField{
}
- (void)textFieldDidEndEditing:(UITextField *)textField{
}
- 通過通知
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(beginEditing)
name:UITextFieldTextDidBeginEditingNotification
object:textField];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(endEditing)
name:UITextFieldTextDidEndEditingNotification
object:textField];
- (void)dealloc{
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
- 重寫UITextField的
becomeFirstResponder
和resignFirstResponder
方法
//調(diào)用時(shí)刻 : 成為第一響應(yīng)者(開始編輯\彈出鍵盤\獲得焦點(diǎn))
- (BOOL)becomeFirstResponder{
return [super becomeFirstResponder];
}
//調(diào)用時(shí)刻 : 不做第一響應(yīng)者(結(jié)束編輯\退出鍵盤\失去焦點(diǎn))
- (BOOL)resignFirstResponder{
return [super resignFirstResponder];
}
UITextField的常見需求
- 更改光標(biāo)的顏色
textField.tintColor = [UIColor whiteColor];
- 設(shè)置占位文字:設(shè)置
placeholder
或者attributedPlaceholder
- 自定義占位文字顏色
- 使用
attributedPlaceholder
進(jìn)行設(shè)置NSMutableDictionary *attributes = [NSMutableDictionary dictionary]; attributes[NSForegroundColorAttributeName] = [UIColor whiteColor]; self.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"Placeholder" attributes:attributes];
- 重寫drawPlaceholderInRect方法
//前提是先設(shè)置placeholder占位文字 否則這個(gè)方法不會(huì)走 - (void)drawPlaceholderInRect:(CGRect)rect{ // 文字屬性 NSMutableDictionary *attrs = [NSMutableDictionary dictionary]; attrs[NSForegroundColorAttributeName] = [UIColor whiteColor]; attrs[NSFontAttributeName] = self.font; CGPoint placeholderPoint = CGPointMake(0, (rect.size.height - self.font.lineHeight) * 0.5); [self.placeholder drawAtPoint:placeholderPoint withAttributes:attrs]; // 畫出占位文字 // CGRect placeholderRect; // placeholderRect.size.width = rect.size.width; // placeholderRect.size.height = self.font.lineHeight; // placeholderRect.origin.x = 0; // placeholderRect.origin.y = (rect.size.height - self.font.lineHeight) * 0.5; // [self.placeholder drawInRect:placeholderRect withAttributes:attrs]; }
- 修改內(nèi)部占位文字Label的文字顏色
[textField setValue:[UIColor grayColor] forKeyPath:@"placeholderLabel.textColor"];
- 在有多個(gè)textfield的時(shí)候在聚焦的時(shí)候占位文字顏色是一種顏色,非聚焦的時(shí)候是另一種顏色:實(shí)現(xiàn)方式是在成為第一響應(yīng)者的時(shí)候設(shè)置一次占位文字顏色,在市區(qū)第一響應(yīng)者的時(shí)候設(shè)置一次占位文字顏色