-(void)textFieldDidBeginEditing:(UITextField *)textField{
NSLog(@"textField DidBeginEditing");
}
-(void)textFieldDidEndEditing:(UITextField *)textField{
NSLog(@"textField DidEndEditing");
}
-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
NSLog(@"textField ShouldBeginEditing");
return YES;
}
-(BOOL)textFieldShouldEndEditing:(UITextField *)textField{
NSLog(@"textField ShouldEndEditing");
return YES;
}
设置两个UITextfield,分别命名txtA和txtB,并先后点击,做个小测试可知:
当第一次点击txtA,执行的是ShouldBeginEditing、DidBeginEditing。
此时并未resign txtA,再去点击txtB,可得ShouldBeginEditing、ShouldEndEditing、DidEndEditing、DidBeginEditing。
既然到此,再稍微总结下UItextfield这个控件吧,毕竟还是很常用的。
之前提到7个方法中的另外三个方法分别是:
-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string;
-(BOOL)textFieldShouldReturn:(UITextField *)textField;
-(BOOL)textFieldShouldClear:(UITextField *)textField;
其中方法一是调用于键盘输入之后,textfield中显示出来之前,实时调用。主要用于是否修正输入的内容。
返回NO代表不允许修改(即满足NO条件的任何键盘输入都无效,但也可以在此设置textfield的值)。返回YES代表你刚才的键盘录入都是Ok的。
string是此时输入的数据,即将会添加到textfield.text后面的数据。
举个例子:
//控制输入内容的长度不超过5。length从0开始计数。
-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
int MAXLENGTH = 5;
if ([textField.text length] >= MAXLENGTH)
{
textField.text = [textField.text substringToIndex:MAXLENGTH];
return NO;
}
return YES;
}
方法二textFieldShouldReturn调用于当点击return按钮的时候,用来控制当点击返回按钮时的一些操作。主要还是调用[textfield resignFirstResponder];
return按钮有几种设置方式:
typedef enum {
UIReturnKeyDefault, 默认 灰色按钮,标有Return
UIReturnKeyGo, 标有Go的蓝色按钮
UIReturnKeyGoogle,标有Google的蓝色按钮,用语搜索
UIReturnKeyJoin,标有Join的蓝色按钮
UIReturnKeyNext,标有Next的蓝色按钮
UIReturnKeyRoute,标有Route的蓝色按钮
UIReturnKeySearch,标有Search的蓝色按钮
UIReturnKeySend,标有Send的蓝色按钮
UIReturnKeyYahoo,标有Yahoo的蓝色按钮
UIReturnKeyYahoo,标有Yahoo的蓝色按钮
UIReturnKeyEmergencyCall, 紧急呼叫按钮
} UIReturnKeyType;
方法三textFieldShouldClear调用于当点击clear按钮的时候。但是clear按钮默认是不显示的,需要去storyboard设置clear button。也可以使用代码控制:
textField.clearButtonMode = UITextFieldViewModeWhileEditing; //编辑时会出现个修改X
它的可选项有:
typedef enum {
UITextFieldViewModeNever, 重不出现
UITextFieldViewModeWhileEditing, 编辑时出现
UITextFieldViewModeUnlessEditing, 除了编辑外都出现
UITextFieldViewModeAlways 一直出现
} UITextFieldViewMode;
/*
*华丽丽的分界线 ----一些神秘的事情
*/
一、UITextfield可以设置左右view,实例:
self.txtFieldA.rightView = [UIButtonbuttonWithType:UIButtonTypeContactAdd];
self.txtFieldA.rightViewMode =UITextFieldViewModeAlways;
其中rightViewMode可选为typedef enum {
UITextFieldViewModeNever,
UITextFieldViewModeWhileEditing,
UITextFieldViewModeUnlessEditing,
UITextFieldViewModeAlways
} UITextFieldViewMode;
二、
//每输入一个字符就变成点 用语密码输入
self.txtFieldA.secureTextEntry = YES;
//是否纠错
self.txtFieldA.autocorrectionType = UITextAutocorrectionTypeNo;
typedef enum {
UITextAutocorrectionTypeDefault, 默认
UITextAutocorrectionTypeNo, 不自动纠错
UITextAutocorrectionTypeYes, 自动纠错
} UITextAutocorrectionType;
//再次编辑就清空
self.txtFieldA.clearsOnBeginEditing = YES;
//首字母是否大写
text.autocapitalizationType = UITextAutocapitalizationTypeNone;
typedef enum {
UITextAutocapitalizationTypeNone, 不自动大写
UITextAutocapitalizationTypeWords, 单词首字母大写
UITextAutocapitalizationTypeSentences, 句子的首字母大写
UITextAutocapitalizationTypeAllCharacters, 所有字母都大写
} UITextAutocapitalizationType;
三:界面重写绘制行为
除了UITextField对象的风格选项,你还可以定制化UITextField对象,为他添加许多不同的重写方法,来改变文本字段的显示行为。这些方法都会返回一个CGRect结构,制定了文本字段每个部件的边界范围。以下方法都可以重写。
– textRectForBounds: //重写来重置文字区域
– drawTextInRect: //改变绘文字属性.重写时调用super可以按默认图形属性绘制,若自己完全重写绘制函数,就不用调用super了.
– placeholderRectForBounds: //重写来重置占位符区域
– drawPlaceholderInRect: //重写改变绘制占位符属性.重写时调用super可以按默认图形属性绘制,若自己完全重写绘制函数,就不用调用super了.
– borderRectForBounds: //重写来重置边缘区域
– editingRectForBounds: //重写来重置编辑区域
– clearButtonRectForBounds: //重写来重置clearButton位置,改变size可能导致button的图片失真
– leftViewRectForBounds:
– rightViewRectForBounds:
代替输入
inputView //代替标准的系统键盘
inputAccessoryView //编辑时显示在系统键盘或用户自定义的inputView上面的视图
四、通知
UITextField派生自UIControl,所以UIControl类中的通知系统在文本字段中也可以使用。除了UIControl类的标准事件,你还可以使用下列UITextField类特有的事件
UITextFieldTextDidBeginEditingNotification
UITextFieldTextDidChangeNotification
UITextFieldTextDidEndEditingNotification
当文本字段退出编辑模式时触发。通知的object属性存储了最终文本。
因为文本字段要使用键盘输入文字,所以下面这些事件发生时,也会发送动作通知
UIKeyboardWillShowNotification //键盘显示之前发送
UIKeyboardDidShowNotification //键盘显示之后发送
UIKeyboardWillHideNotification //键盘隐藏之前发送
UIKeyboardDidHideNotification //键盘隐藏之后发送