初始化
UITextField *textField = [[UITextField alloc]init];
textField.frame = CGRectMake(100,100, 200,20);
textField.placeholder = @"請輸入信息";
[self.view addSubview:textField];
常用方法和屬性
常用方法和屬性 | 解釋 |
---|---|
textField.placeholder = @“請輸入信息”; | 設置佔位文字 |
textField.tintColor = [UIColor orangeColor]; | 設置光標顏色 |
textField.borderStyle = UITextBorderStyleLine; | 設置邊框樣式 |
textField.autocorrectionType = UITextAutocorrectionTypeNo; | 設置自動校正 |
textField.autocapitalizationType = UITextAutocapitalizationTypeAllCharacters; | 設置自動大寫 |
textField.returnKeyType = UIReturnKeyYahoo; | 設置鍵盤返回按鈕 |
textField.enablesReturnKeyAutomatically = YES; | 設置鍵盤返回按鈕不可點(當輸入框內無文字時,返回按鈕不可點) |
textField.clearButtonMode = UITextFieldViewModeWhileEditing; | 設置右側全部清除按鈕 |
[textField setValue:[UIColor redColor] forKeyPath:@"_placeholderLabel.textColor"] | 修改textField的placeholder的字體顏色 |
[textField setValue:[UIFont boldSystemFontOfSize:16] forKeyPath:@"_placeholderLabel.font"] | 修改textField的placeholder的字體大小 |
設置佔位文字樣式
NSMutableDictionary *dict = [NSMutableDictionary dictionary];
dict[NSForegroundColorAttributeName] = [UIColor redColor];
NSAttributedString *attrStr = [[NSAttributedString alloc]initWithString:@"佔位文字" attributes:dict];
textField.attributedPlaceholder = attrStr;
代理相關方法
- (void)textFieldDidBeginEditing:(UITextField *)textField;//開始編輯 獲取焦點
- (void)textFieldDidEndEditing:(UITextField *)textField;//結束編輯 失去焦點
- (BOOL)textFieldShouldReturn:(UITextField *)textField;//點擊鍵盤返回按鈕
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string;//文本發生變化時觸發此方法
監聽UITextField的文字改變不建議使用代理, 用addTarget監聽文字改變事件。
[textField addTarget:self action:@selector(textEditingChanged) forControlEvents:UIControlEventEditingChanged];
UITextField通知
UITextFieldTextDidBeginEditingNotification //開始編輯時
UITextFieldTextDidEndEditingNotification //結束編輯時
UITextFieldTextDidChangeNotification //文本發生變化時
UIKeyboard通知
UIKeyboardWillShowNotification //鍵盤顯示之前發送
UIKeyboardDidShowNotification //鍵盤顯示之後發送
UIKeyboardWillHideNotification //鍵盤隱藏之前發送
UIKeyboardDidHideNotification //鍵盤隱藏之後發送
鍵盤類型
textField.keyboardType = UIKeyboardTypeNumberPad;
typedef enum {
UIKeyboardTypeDefault, 默認鍵盤,支持所有字符
UIKeyboardTypeASCIICapable, 支持ASCII的默認鍵盤
UIKeyboardTypeNumbersAndPunctuation, 標準電話鍵盤,支持+*#字符
UIKeyboardTypeURL, URL鍵盤,支持.com按鈕 只支持URL字符
UIKeyboardTypeNumberPad, 數字鍵盤
UIKeyboardTypePhonePad, 電話鍵盤
UIKeyboardTypeNamePhonePad, 電話鍵盤,也支持輸入人名
UIKeyboardTypeEmailAddress, 用於輸入電子 郵件地址的鍵盤
UIKeyboardTypeDecimalPad, 數字鍵盤 有數字和小數點
UIKeyboardTypeTwitter, 優化的鍵盤,方便輸入@、#字符
UIKeyboardTypeAlphabet = UIKeyboardTypeASCIICapable,
} UIKeyboardType;
限制只能輸入特定字符
方式1
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
NSCharacterSet *cs;
//invertedSet方法是去反字符,把所有的除了number 裏的字符都找出來(包含去空格功能)
NSString *number = @"1234567890";
cs = [[NSCharacterSet characterSetWithCharactersInString:number]invertedSet];
NSString *filtered = [[string componentsSeparatedByCharactersInSet:cs]componentsJoinedByString:@""];
BOOL canChange = [string isEqualToString:filtered];
return canChange;
}
方式2(正則表達式)
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
NSString* number= @"^[0-9]*$";
NSPredicate *numberPre = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",number];
return [numberPre evaluateWithObject:string];
}
限制輸入字符長度
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
NSString * toBeString = [textField.text stringByReplacingCharactersInRange:range withString:string];
if ([toBeString length] > 10) {
textField.text = [toBeString substringToIndex:10];
return NO;
}
return YES;
}
點擊頁面空白處關閉鍵盤
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(viewTapped:)];
[self.view addGestureRecognizer:tap];
-(void)viewTapped:(UITapGestureRecognizer*)tap{
[self.view endEditing:YES];
}
更多屬性 可以查看UIKit/Headers/NSAttributedString.h文件。