UITextField相关基础

UITextField *TF = [[UITextField alloc] initWithFrame:CGRectMke(90,100,140,25)];//UITextField 初始化:

[self.window addSubview:TF];// 添加到视图

TF.textColor = [UIColor  redColor];// 输入的字体颜色

TF.delegate = self;//设置代理,要在.h文件上添加代理  @interface  AppDelegate:UIResponder 

(代理的用处: 1.监听那些不能通过addTarget监听的事件!

2.主要用来负责在两个对象之间,发生某些事件时,来传递消息或者数据)

TF.placeholder = @“请输入密码: ”;// 当没有输入内容时,有水印字体提示

TF.textColor = [UIColor  reColor ];//输入字体的颜色

TF.secureTextEntry = YES;//输入内容是否可见

TF.tag = 101;// 给个tag值

TF.adjustsFontSizeToFitWidth = YES;//当输入字符长度大于框架长度时,自动缩小字符以适应框架长度

TF.background = [UIImage imageNamed:@“***.jpg”];//背景,当使用图片为背景时,需要把TF.borderStyle设置为None

TF.backgroundColor = [UIColor yellowColor];//当背景为颜色时,TF.borderStyle不能设为None

TF.borderStyle = UITextBorderStyleNone;//没有样式,直角边框

UITextBorderStyleLine;//边框为黑边,直角边框

UITextBorderStyleBezel;//有点阴影,直角边框

UITextBorderStyleRoundedRect;// 圆角框

[TF becomeFirstResponder];// 当渲染视图后,TF自动成为第一响应者,弹出键盘,也可以在代理方法里进行

// 输入框里的叉号,出现形式

TF.clearButtonMode = UITextFieldViewModeWhileEding;// 编辑时出现

UITextFieldViewModeNever;//从不出现

UITextFieldViewModeUnlessEditing;// 除了编辑时,其他时间都出现

UITextFieldViewModeAlways;// 一直都出现

TF.autocorrectionType = UITextAutocorrectionTypeNo;//不自动纠错

UITextAutocorrectionTypeDefault;//默认

UITextAutocorrectionTypeYes;//自动纠错

TF.clearsOnBeginEditing = YES;//重新点击编辑时,是否清空上次输入的内容,后面也有代理方法

//设置弹出的键盘的类型

TF.keyboardType = UIKeyboardTypeDefault;//默认键盘,支持所有字符

UIKeyboardTypeASCIICapable;//支持ASCII的默认键盘

UIKeyboardTypeNumbersAndPunctuation ;//标准电话键盘,支持+*#字符

UIKeyboardTypeURL;//URL键盘,支持.com按钮 只支持URL字符

UIKeyboardTypeNumberPad;//数字键盘

UIKeyboardTypePhonePad;//电话键盘

UIKeyboardTypeNamePhnoePad;//电话键盘,也支持输入人名

UIKeyboardTypeEmailAddress;//电子邮件键盘

UIKeyboardTypeDecimalPad;//数字键盘,有小数点

UIKeyboardTypeTwitter;//优化键盘

//首字母是否大写

TF.autocapitalizationType = UITextAutocapitalizationTypeNone;//不自动大写

UITextAutocapitalizationTypeWords,;//单词首字母大写

UITextAutocapitalizationTypeSentences;//句子的首字母大写

UITextAutocapitalizationTypeAllCharacters;// 所有字母都大写

//return键变成什么键

TF.returnKeyType =UIReturnKeyDone;//标有Done的蓝色按钮

UIReturnKeyDefault;//默认 灰色按钮,标有Return

UIReturnKeyGo;//标有Go的蓝色按钮

UIReturnKeyGoogle;//标有Google的蓝色按钮,用语搜索

UIReturnKeyJoin;//标有Join的蓝色按钮

UIReturnKeyNext;//标有Next的蓝色按钮

UIReturnKeyRoute;//标有Route的蓝色按钮

UIReturnKeySearch;//标有Search的蓝色按钮

UIReturnKeySend;//标有Send的蓝色按钮

UIReturnKeyYahoo;//标有Yahoo的蓝色按钮

UIReturnKeyYahoo;//标有Yahoo的蓝色按钮

UIReturnKeyEmergencyCall;//紧急呼叫按钮

//UITextField的代理方法  (需要先设置代理)   注意是否要设置返回值

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField;//当用户点击到,并准备开始编辑内容时,使用方法,一边是弹出窗口

- (void)textFieldDidBeginEditing:(UITextField *)textField;//当用户开始编辑时,使用此代理方法

- (BOOL)textFieldShouldEndEditing:(UITextField *)textField;//当用户准备结束编辑时,使用此代理方法

- (void)textFieldDidEndEditing:(UITextField *)textField;//当用户结束编辑时,使用此代理方法

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string

{

if (range.location >= 6) {

return NO;

}else

return YES;

}// 当用户使用自动更正功能,把输入的文字修改为推荐的文字时,又或者限制输入长度时, 就会调用这个方法。

- (BOOL)textFieldShouldClear:(UITextField *)textField;//当使用清除输入内容功能时,使用此代理方法

- (BOOL)textFieldShouldReturn:(UITextField *)textField;//当点击键盘里的Return收起键盘时,使用此代理方法

键盘的弹出及消失流程:

以UITextField的键盘弹出顺序为例:

先是[textField becomeFirstResponsder]----------->

调用textField的代理事件(textFieldShouldBeginEditing)----------->

假如textFieldShouldBeginEditing返回YES,,调用代理事件textFieldDidBeginEditing,那么[textField

isFirstResponsder]返回YES------------>

键盘开始弹出,发送键盘出现通知 UIKeyboardWillShowNotification,当键盘完全出现在屏幕上时,发送UIKeyboardDidShowNotification

键盘消失顺序(与弹出顺序类似):

先是[textField resignFirstResponsder]----------->

调用(textFieldShouldEndEditing)--------------->

假如textFieldShouldEndEditing返回YES,调用textFieldDidEndEditing,[textField isFirstResponsder]返回NO,------------->

键盘消失,发送键盘消失通知 UIKeyboardWillHideNotification,当键盘完全消失在屏幕上时,发送UIKeyboardDidHideNotification

你可能感兴趣的:(UITextField相关基础)