1、基本设置
键盘类型
要显示中文,还得设置info.plist中
Localization native development region 为 cn
UITextField *textF = [[UITextField alloc]init];
//需要先把样式给取消掉
textF.borderStyle = UITextBorderStyleNone;
//然后再设置背景图片
textF.background = [UIImage imageNamed:@"chat_send_nor"];
//设置背景
text.disabledBackground = [UIImage imageNamed:@"cc.png"];
//光标如果太靠近边框,可以进行如下设置
UIView *leftView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 10, 0)];
textF.leftView = leftView;
textF.leftViewMode = UITextFieldViewModeAlways;
//占位文字颜色
[textF setValue:[UIColor redColor] forKeyPath:@"_placeholderLabel.textColor"];
textF.tintColor = [UIColor blackColor];
输入框中是否有个叉号,在什么时候显示,用于一次性删除输入框中的内容
//输入框中是否有个叉号,在什么时候显示,用于一次性删除输入框中的内容
text.clearButtonMode = UITextFieldViewModeAlways;
typedef enum {
UITextFieldViewModeNever, 重不出现
UITextFieldViewModeWhileEditing, 编辑时出现
UITextFieldViewModeUnlessEditing, 除了编辑外都出现
UITextFieldViewModeAlways 一直出现
} UITextFieldViewMode;
是否纠错
//是否纠错
text.autocorrectionType = UITextAutocorrectionTypeNo;
typedef enum {
UITextAutocorrectionTypeDefault, 默认
UITextAutocorrectionTypeNo, 不自动纠错
UITextAutocorrectionTypeYes, 自动纠错
} UITextAutocorrectionType;
再次编辑就清空
//再次编辑就清空
text.clearsOnBeginEditing = YES;
对齐方式
//内容对齐方式
text.textAlignment = UITextAlignmentLeft;
//内容的垂直对齐方式 UITextField继承自UIControl,此类中有一个属性contentVerticalAlignment
_countryTextF.contentVerticalAlignment = UIControlContentVerticalAlignmentTop;
内容填充
//设置为YES时文本会自动缩小以适应文本窗口大小.默认是保持原来大小,而让长文本滚动
textFied.adjustsFontSizeToFitWidth = YES;
//设置自动缩小显示的最小字体大小
text.minimumFontSize = 20;
_countryTextF.font = [UIFont systemFontOfSize:25];
首字母是否大写
//首字母是否大写
text.autocapitalizationType = UITextAutocapitalizationTypeNone;
typedef enum {
UITextAutocapitalizationTypeNone, 不自动大写
UITextAutocapitalizationTypeWords, 单词首字母大写
UITextAutocapitalizationTypeSentences, 句子的首字母大写
UITextAutocapitalizationTypeAllCharacters, 所有字母都大写
} UITextAutocapitalizationType;
最右侧加图片是以下代码 左侧类似
//最右侧加图片是以下代码 左侧类似
UIImageView *image=[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"right.png"]];
text.rightView=image;
text.rightViewMode = UITextFieldViewModeAlways;
typedef enum {
UITextFieldViewModeNever,
UITextFieldViewModeWhileEditing,
UITextFieldViewModeUnlessEditing,
UITextFieldViewModeAlways
} UITextFieldViewMode;
设置键盘背景颜色
self.countryTextF.keyboardAppearance = UIKeyboardAppearanceDark;
self.birthDayTextF.keyboardAppearance = UIKeyboardAppearanceLight;
shift按键
textView.autocapitalizationType = UITextAutocapitalizationTypeNone;//shift不能自动有效
textView.autocapitalizationType = UITextAutocapitalizationTypeWords;//单词开头的情况下自动有效,就是每输入一个单词在开头的时候自动大写,第二个字母开始就自动小写
textView.autocapitalizationType = UITextAutocapitalizationTypeSentences;//文章开头有效
textView.autocapitalizationType = UITextAutocapitalizationTypeAllCharacters;//一直有效(就是一直大写)
自动矫正功能
_countryTextF.autocorrectionType = UITextAutocorrectionTypeYes;
2、
//是否允许开始编辑
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
return YES;
}
//开始编辑时调用(成为第一响应者)
// became first responder
- (void)textFieldDidBeginEditing:(UITextField *)textField{
//NSLog(@"%s",__func__);
}
//是否允许结束编辑
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField{
return YES;
}
//当结束编辑时调用
- (void)textFieldDidEndEditing:(UITextField *)textField{
NSLog(@"%s",__func__);
}
//是否允许改变文本框内容(拦截用户输入)
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
return NO;
}
监听return处理(没有内容则无法点击)
self.countryTextF.enablesReturnKeyAutomatically = YES;
self.countryTextF.returnKeyType = UIReturnKeyNext;
self.birthDayTextF.returnKeyType = UIReturnKeyGoogle;
self.cityTextF.returnKeyType = UIReturnKeyContinue;
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
NSLog(@"%s",__func__);
return YES;
}
typedef enum {
UIReturnKeyDefault, 默认 灰色按钮,标有Return
UIReturnKeyGo, 标有Go的蓝色按钮
UIReturnKeyGoogle,标有Google的蓝色按钮,用语搜索
UIReturnKeyJoin,标有Join的蓝色按钮
UIReturnKeyNext,标有Next的蓝色按钮
UIReturnKeyRoute,标有Route的蓝色按钮
UIReturnKeySearch,标有Search的蓝色按钮
UIReturnKeySend,标有Send的蓝色按钮
UIReturnKeyYahoo,标有Yahoo的蓝色按钮
UIReturnKeyYahoo,标有Yahoo的蓝色按钮
UIReturnKeyEmergencyCall, 紧急呼叫按钮
} UIReturnKeyType;
3、监听文字改变
方式1
[textField addTarget:self action:@selector(textEditingChanged:) forControlEvents:UIControlEventEditingChanged];
- (void)textEditingChanged:(UITextField *)textField {
NSLog(@"%@",textField.text);
}
方式2
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(changeTextField:) name:UITextFieldTextDidChangeNotification object:textF];
- (void)changeTextField:(NSNotification *)notification {
UITextField *textField = [notification object];
NSLog(@"%@",textField.text);
}
代理监听
- 此代理只能输出==上次已有==的字符(本次点击键盘输入的文字不会被输出),所以我认为此代理==适合做输入字符串内容的控制==
只能输入数字
``` - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
NSCharacterSet *charSet = [[NSCharacterSet characterSetWithCharactersInString:@”0123456789”] invertedSet];
NSString *filteredStr = [[string componentsSeparatedByCharactersInSet:charSet] componentsJoinedByString:@””];
if ([string isEqualToString:filteredStr]) {
}return YES;
return NO;##### 输入固定位数
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
NSString *numberString = [textField.text stringByReplacingCharactersInRange:range withString:string];
if (numberString.length > 5) {
}textField.text = [numberString substringToIndex:5]; return NO;
return YES;
}
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
if (textField == self.liveThemeTextField) {
//这里的if时候为了获取删除操作,如果没有此if会造成当达到字数限制后删除键也不能使用的后果.
if (range.length == 1 && string.length == 0) {
return YES;
}
//so easy
else if (self.liveThemeTextField.text.length >= 30) {
self.liveThemeTextField.text = [textField.text substringToIndex:30];
return NO;
}
}
return YES;
}
swift(2018年更新)
当用户输入达到最大限制字数的时候,此时的删除按钮不能使用
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
if range.length == 1 && string.count == 0 {
return true
}
if (textField.text?.count ?? 0) > 10 {
return false
}
return true
}