初始化
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文件。