//1
、导入代理的名字
@interface AppDelegate ()< UITextFieldDelegate >
@end
@implementation AppDelegate
- ( BOOL )application:( UIApplication *)application didFinishLaunchingWithOptions:( NSDictionary *)launchOptions {
/*
UITextField->UIControl 的子类 ->UIView 的子类
文本输入框
text :可以获得或者改变输入框的文字内容
placeholder 提示文本
textColor: 字体颜色
font: 字号
textAlignment: 对齐方式
adjustsFontSizeToFitWidth: 让文字自动根据宽度适配
minimumFontSize :设置文字的最小字号
clearButtonMode :设置清除按钮什么时候显示
leftView :左侧视图 ——> 输入框左侧视图
rightView: 右侧视图 -> 输入框右侧视图
****** 并不是设置了左侧右侧视图就可以显示出来需要配合使用下面属性
leftViewMode :设置什么情况下 显示左侧视图
rightViewMode :设置什么情况下 显示右侧视图
inputView :键盘上面的视图
inputAccessoryView :键盘区域的视图
borderStyle: 输入框的样式
输入框的方法(代理方法)
// 开始编辑的调用
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField; // return NO to disallow editing.
- (void)textFieldDidBeginEditing:(UITextField *)textField; // became first responder
// 结束编辑的时候调用
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField; // return YES to allow editing to stop and to resign first responder status. NO to disallow the editing session to end
- (void)textFieldDidEndEditing:(UITextField *)textField; // may be called if forced even if shouldEndEditing returns NO (e.g. view removed from window) or endEditing:YES called
// 当文字内容发生改变的时候调用这个方法
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string; // return NO to not change text
// 点击清除按钮的时候调用
- (BOOL)textFieldShouldClear:(UITextField *)textField; // called when clear button pressed. return NO to ignore (no notifications)
// 点击 return 键的时候调用
- (BOOL)textFieldShouldReturn:(UITextField *)textField; // called when 'return' key pressed. return NO to ignore.
// 专门为输入框准备的响应时间
UIControlEventEditingDidBegin = 1 << 16, // UITextField
UIControlEventEditingChanged = 1 << 17,
UIControlEventEditingDidEnd = 1 << 18,
UIControlEventEditingDidEndOnExit = 1 << 19, // 'return key' ending editing
注意点:
1 、如果想使用代理方法必须先导入代理方法
2 、如果代理方法没有触发 看是否 挂上代理
在使用左右侧视图的时候要配合左右视图的 model 来使用
keyboardType :键盘的样式
UIReturnKeyDefault,
UIReturnKeyGo,
UIReturnKeyGoogle,
UIReturnKeyJoin,
UIReturnKeyNext,
UIReturnKeyRoute,
UIReturnKeySearch,
UIReturnKeySend,
UIReturnKeyYahoo,
UIReturnKeyDone,
UIReturnKeyEmergencyCall,
*/
[ self . window makeKeyAndVisible ];
UITextField *accTextField = [[ UITextField alloc ] initWithFrame : CGRectMake ( 10 , 40 , CGRectGetWidth ([ UIScreen mainScreen ]. bounds )- 20 , 40 )];
// accTextField.backgroundColor = [UIColor orangeColor];
// 设置输入框的文字
accTextField. borderStyle = UITextBorderStyleLine ;
//
/*
UITextBorderStyleNone,// 没有样式
UITextBorderStyleLine,// 黑线边框
UITextBorderStyleBezel, 黑线加阴影
UITextBorderStyleRoundedRect// 圆角
*/
// 提示文字
accTextField. placeholder = @" 请输入账号 " ;
// 清除按钮
accTextField. clearButtonMode = UITextFieldViewModeWhileEditing ;
/* UITextFieldViewModeNever,// 永远都不显示
UITextFieldViewModeWhileEditing, 当编辑的时候显示
UITextFieldViewModeUnlessEditing,// 不在编辑的时候显示
UITextFieldViewModeAlways// 永远显示
*/
UIImageView *leftImageView = [[ UIImageView alloc ] initWithFrame : CGRectMake ( 0 , 0 , 25 , 30 )];
leftImageView. image = [ UIImage imageNamed : @"user_avatar_704px_1195968_easyicon.net.png" ];
accTextField. leftView = leftImageView;
accTextField. leftViewMode = UITextFieldViewModeAlways ;
UIView *inputView1 = [[ UIView alloc ] initWithFrame : CGRectMake ( 0 , 0 , CGRectGetWidth ([ UIScreen mainScreen ]. bounds ), 40 )];
inputView1. backgroundColor = [ UIColor yellowColor ];
// accTextField.inputView = inputView1;
// accTextField.inputAccessoryView = inputView1;
// 想使用代理方法 必须挂上代理
accTextField. delegate = self ;
// - (BOOL)textFieldShouldBeginEditing:(UITextField *)textField; // return NO to disallow editing.
// - (void)textFieldDidBeginEditing:(UITextField *)textField; // became first responder
// - (BOOL)textFieldShouldEndEditing:(UITextField *)textField; // return YES to allow editing to stop and to resign first responder status. NO to disallow the editing session to end
// - (void)textFieldDidEndEditing:(UITextField *)textField; // may be called if forced even if shouldEndEditing returns NO (e.g. view removed from window) or endEditing:YES called
//
// - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string; // return NO to not change text
//
// - (BOOL)textFieldShouldClear:(UITextField *)textField; // called when clear button pressed. return NO to ignore (no notifications)
accTextField. keyboardType = UIKeyboardTypeNamePhonePad ;
/*
UIKeyboardTypeDefault,
UIKeyboardTypeASCIICapable,
UIKeyboardTypeNumbersAndPunctuation,
UIKeyboardTypeURL,// 有网址类型的
UIKeyboardTypeNumberPad,//iPad 专用 只显示几个数字
UIKeyboardTypeNamePhonePad,// 电话类型有 1~9 个数字( * 0 # )
UIKeyboardTypeEmailAddress, 邮箱地址类型
UIKeyboardTypeDecimalPad
UIKeyboardTypeTwitter
UIKeyboardTypeWebSearch // 网页搜索 有小地球
UIKeyboardTypeAlphabet = UIKeyboardTypeASCIICapable,
*/
[ self . window addSubview :accTextField];
return YES ;
}
// 开始编辑
- ( void )textFieldDidBeginEditing:( UITextField *)textField{
NSLog ( @" 开始编辑 " );
}
// 可以获得用户每输入的一次文字、位置
- ( BOOL )textField:( UITextField *)textField shouldChangeCharactersInRange:( NSRange )range replacementString:( NSString *)string{
NSLog ( @"%@" ,string);
return YES ;
}
// 点击清除按钮调用
- ( BOOL )textFieldShouldClear:( UITextField *)textField{
NSLog ( @" 清除按钮 " );
return YES ;
}
// 编辑结束的时候调用
- ( void )textFieldDidEndEditing:( UITextField *)textField{
NSLog ( @"%@" ,textField. text );
}
- ( BOOL )textFieldShouldEndEditing:( UITextField *)textField{
return YES ;
}
// 当点击 return 键的时候触发
- ( BOOL )textFieldShouldReturn:( UITextField *)textField{
// 回收键盘 -> 输入结束
[textField resignFirstResponder ];
return YES ;
}
@interface AppDelegate ()< UITextFieldDelegate >
@end
@implementation AppDelegate
- ( BOOL )application:( UIApplication *)application didFinishLaunchingWithOptions:( NSDictionary *)launchOptions {
/*
UITextField->UIControl 的子类 ->UIView 的子类
文本输入框
text :可以获得或者改变输入框的文字内容
placeholder 提示文本
textColor: 字体颜色
font: 字号
textAlignment: 对齐方式
adjustsFontSizeToFitWidth: 让文字自动根据宽度适配
minimumFontSize :设置文字的最小字号
clearButtonMode :设置清除按钮什么时候显示
leftView :左侧视图 ——> 输入框左侧视图
rightView: 右侧视图 -> 输入框右侧视图
****** 并不是设置了左侧右侧视图就可以显示出来需要配合使用下面属性
leftViewMode :设置什么情况下 显示左侧视图
rightViewMode :设置什么情况下 显示右侧视图
inputView :键盘上面的视图
inputAccessoryView :键盘区域的视图
borderStyle: 输入框的样式
输入框的方法(代理方法)
// 开始编辑的调用
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField; // return NO to disallow editing.
- (void)textFieldDidBeginEditing:(UITextField *)textField; // became first responder
// 结束编辑的时候调用
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField; // return YES to allow editing to stop and to resign first responder status. NO to disallow the editing session to end
- (void)textFieldDidEndEditing:(UITextField *)textField; // may be called if forced even if shouldEndEditing returns NO (e.g. view removed from window) or endEditing:YES called
// 当文字内容发生改变的时候调用这个方法
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string; // return NO to not change text
// 点击清除按钮的时候调用
- (BOOL)textFieldShouldClear:(UITextField *)textField; // called when clear button pressed. return NO to ignore (no notifications)
// 点击 return 键的时候调用
- (BOOL)textFieldShouldReturn:(UITextField *)textField; // called when 'return' key pressed. return NO to ignore.
// 专门为输入框准备的响应时间
UIControlEventEditingDidBegin = 1 << 16, // UITextField
UIControlEventEditingChanged = 1 << 17,
UIControlEventEditingDidEnd = 1 << 18,
UIControlEventEditingDidEndOnExit = 1 << 19, // 'return key' ending editing
注意点:
1 、如果想使用代理方法必须先导入代理方法
2 、如果代理方法没有触发 看是否 挂上代理
在使用左右侧视图的时候要配合左右视图的 model 来使用
keyboardType :键盘的样式
UIReturnKeyDefault,
UIReturnKeyGo,
UIReturnKeyGoogle,
UIReturnKeyJoin,
UIReturnKeyNext,
UIReturnKeyRoute,
UIReturnKeySearch,
UIReturnKeySend,
UIReturnKeyYahoo,
UIReturnKeyDone,
UIReturnKeyEmergencyCall,
*/
[ self . window makeKeyAndVisible ];
UITextField *accTextField = [[ UITextField alloc ] initWithFrame : CGRectMake ( 10 , 40 , CGRectGetWidth ([ UIScreen mainScreen ]. bounds )- 20 , 40 )];
// accTextField.backgroundColor = [UIColor orangeColor];
// 设置输入框的文字
accTextField. borderStyle = UITextBorderStyleLine ;
//
/*
UITextBorderStyleNone,// 没有样式
UITextBorderStyleLine,// 黑线边框
UITextBorderStyleBezel, 黑线加阴影
UITextBorderStyleRoundedRect// 圆角
*/
// 提示文字
accTextField. placeholder = @" 请输入账号 " ;
// 清除按钮
accTextField. clearButtonMode = UITextFieldViewModeWhileEditing ;
/* UITextFieldViewModeNever,// 永远都不显示
UITextFieldViewModeWhileEditing, 当编辑的时候显示
UITextFieldViewModeUnlessEditing,// 不在编辑的时候显示
UITextFieldViewModeAlways// 永远显示
*/
UIImageView *leftImageView = [[ UIImageView alloc ] initWithFrame : CGRectMake ( 0 , 0 , 25 , 30 )];
leftImageView. image = [ UIImage imageNamed : @"user_avatar_704px_1195968_easyicon.net.png" ];
accTextField. leftView = leftImageView;
accTextField. leftViewMode = UITextFieldViewModeAlways ;
UIView *inputView1 = [[ UIView alloc ] initWithFrame : CGRectMake ( 0 , 0 , CGRectGetWidth ([ UIScreen mainScreen ]. bounds ), 40 )];
inputView1. backgroundColor = [ UIColor yellowColor ];
// accTextField.inputView = inputView1;
// accTextField.inputAccessoryView = inputView1;
// 想使用代理方法 必须挂上代理
accTextField. delegate = self ;
// - (BOOL)textFieldShouldBeginEditing:(UITextField *)textField; // return NO to disallow editing.
// - (void)textFieldDidBeginEditing:(UITextField *)textField; // became first responder
// - (BOOL)textFieldShouldEndEditing:(UITextField *)textField; // return YES to allow editing to stop and to resign first responder status. NO to disallow the editing session to end
// - (void)textFieldDidEndEditing:(UITextField *)textField; // may be called if forced even if shouldEndEditing returns NO (e.g. view removed from window) or endEditing:YES called
//
// - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string; // return NO to not change text
//
// - (BOOL)textFieldShouldClear:(UITextField *)textField; // called when clear button pressed. return NO to ignore (no notifications)
accTextField. keyboardType = UIKeyboardTypeNamePhonePad ;
/*
UIKeyboardTypeDefault,
UIKeyboardTypeASCIICapable,
UIKeyboardTypeNumbersAndPunctuation,
UIKeyboardTypeURL,// 有网址类型的
UIKeyboardTypeNumberPad,//iPad 专用 只显示几个数字
UIKeyboardTypeNamePhonePad,// 电话类型有 1~9 个数字( * 0 # )
UIKeyboardTypeEmailAddress, 邮箱地址类型
UIKeyboardTypeDecimalPad
UIKeyboardTypeTwitter
UIKeyboardTypeWebSearch // 网页搜索 有小地球
UIKeyboardTypeAlphabet = UIKeyboardTypeASCIICapable,
*/
[ self . window addSubview :accTextField];
return YES ;
}
// 开始编辑
- ( void )textFieldDidBeginEditing:( UITextField *)textField{
NSLog ( @" 开始编辑 " );
}
// 可以获得用户每输入的一次文字、位置
- ( BOOL )textField:( UITextField *)textField shouldChangeCharactersInRange:( NSRange )range replacementString:( NSString *)string{
NSLog ( @"%@" ,string);
return YES ;
}
// 点击清除按钮调用
- ( BOOL )textFieldShouldClear:( UITextField *)textField{
NSLog ( @" 清除按钮 " );
return YES ;
}
// 编辑结束的时候调用
- ( void )textFieldDidEndEditing:( UITextField *)textField{
NSLog ( @"%@" ,textField. text );
}
- ( BOOL )textFieldShouldEndEditing:( UITextField *)textField{
return YES ;
}
// 当点击 return 键的时候触发
- ( BOOL )textFieldShouldReturn:( UITextField *)textField{
// 回收键盘 -> 输入结束
[textField resignFirstResponder ];
return YES ;
}