UITextFiled
(1)名称:文本输入框
(2)作用:用于用户输入文本信息(类似于C scanf)的UI视图
(3)继承于UIView
一.基础属性方法
下面列出属性方法的在工程中相关函数
1、创建textField
UITextField *textField=[[UITextField alloc] initWithFrame:CGRectMake(20, 100, self.view.frame.size.width-40, 50)];
2、设置边框样式
textField.borderStyle=UITextBorderStyleLine;
UITextBorderStyleNone, 无边框
UITextBorderStyleLine, 线性边框
UITextBorderStyleBezel,
UITextBorderStyleRoundedRect,矩形边框
3、设置textField背景色
textField.backgroundColor=[UIColor orangeColor];
4、设置textField文字
textField.text=@"文本框";
5、设置文字对齐方式
textField.textAlignment=NSTextAlignmentCenter;
6、文字垂直对齐
textField.contentVerticalAlignment=UIControlContentVerticalAlignmentTop;
UIControlContentVerticalAlignmentCenter 垂直居中
UIControlContentVerticalAlignmentTop 靠上,居顶部
UIControlContentVerticalAlignmentBottom 靠下,居底部
UIControlContentVerticalAlignmentFill 填充
7、设置字体
textField.font=[UIFont systemFontOfSize:40];
8、自适应宽度
textField.adjustsFontSizeToFitWidth=YES;
9、清空按钮(默认是从不清空的)
textField.clearButtonMode=UITextFieldViewModeWhileEditing;
UITextFieldViewModeNever, 从不清空
UITextFieldViewModeWhileEditing, 将要编辑,编辑的时候清空
UITextFieldViewModeUnlessEditing, 除了编辑,都清空
UITextFieldViewModeAlways 永远清空
10、设置密文显示,默认为NO
textField.secureTextEntry=NO;
11、设置键盘自动弹出,或者是,开始进入编辑状态
[textField becomeFirstResponder];
12、设置textField文字颜色
[textField setTextColor:[UIColor blueColor]];
13、(1)设置textField左边视图,默认不显示的
UIView *leftView=[[UIView alloc] initWithFrame:CGRectMake(0, 0, 40, 40)];
leftView.backgroundColor=[UIColor magentaColor];
textField.leftView=leftView;
左视图默认永不显示的
textField.leftViewMode=UITextFieldViewModeAlways;
UITextFieldViewModeNever,
UITextFieldViewModeWhileEditing,
UITextFieldViewModeUnlessEditing,
UITextFieldViewModeAlways
(2)设置textField右边视图,默认不显示的
右视图会覆盖清空按钮
UIView *rightView=[[UIView alloc] initWithFrame:CGRectMake(0, 0, 40, 40)];
rightView.backgroundColor=[UIColor redColor];
textField.rightView=rightView;
textField.rightViewMode=UITextFieldViewModeAlways;
14、设置占位符,通俗地讲,就是提示信息
textField.placeholder=@"用户名";
15、键盘样式,默认Default
textField.keyboardType=UIKeyboardTypeDefault;
UIKeyboardTypeDefault,
UIKeyboardTypeASCIICapable,
UIKeyboardTypeNumbersAndPunctuation,
UIKeyboardTypeURL,
UIKeyboardTypeNumberPad, 数字键盘,ipad
UIKeyboardTypePhonePad, 手机键盘,ipad
UIKeyboardTypeNamePhonePad,
UIKeyboardTypeEmailAddress, 可输入邮件地址的键盘
UIKeyboardTypeDecimalPad NS_ENUM_AVAILABLE_IOS(4_1),
UIKeyboardTypeTwitter NS_ENUM_AVAILABLE_IOS(5_0),
UIKeyboardTypeWebSearch NS_ENUM_AVAILABLE_IOS(7_0),
UIKeyboardTypeAlphabet = UIKeyboardTypeASCIICapable,
16、键盘返回键的样式,默认Default,只有在键盘样式为DEfault的情况下才有效;
textField.returnKeyType=UIReturnKeyGoogle;
UIReturnKeyDefault,
UIReturnKeyGo,
UIReturnKeyGoogle,
UIReturnKeyJoin,
UIReturnKeyNext,
UIReturnKeyRoute,
UIReturnKeySearch,
UIReturnKeySend,
UIReturnKeyYahoo,
UIReturnKeyDone,
UIReturnKeyEmergencyCall,
17、自定义键盘
UIView *inputView=[[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 200)];
inputView.backgroundColor=[UIColor orangeColor];
textField.inputView=inputView;
[inputView release];
18、自定义二级键盘
UIView *inputAccesoryView=[[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 40)];
inputAccesoView.backgroundColor=[UIColor greenColor];
UILabel *label=[[UILabel alloc] initWithFrame:CGRectMake(0, 0, inputAccesoryView.frame.size.width, 40)];
label.text=@"二级键盘";
[inputAccesoryView addSubview:label];
[label release];
textField.inputAccessoryView=inputAccesoryView;
[inputAccesoryView release];
[self.view addSubview:textField];
[textField release];
二.协议中的方法
1、(即将)开始进入编辑
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
2、返回事件的回调方法
- (BOOL)textFieldShouldReturn:(UITextField *)textField
关闭键盘
[textField resignFirstResponder];
3、将要结束编辑
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField
4、将要清空
- (BOOL)textFieldShouldClear:(UITextField *)textField
5、已经进入编辑状态
- (void)textFieldDidBeginEditing:(UITextField *)textField
6、结束编辑状态
- (void)textFieldDidEndEditing:(UITextField *)textField
7、将要改变文字的时候
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
三.监听键盘的收起和弹出
引用:UIKeyboardAnimationDurationUserInfoKey
hideKeyboardNotification隐藏
showKeyboardNotification 显示
1、创建通知中心
- (void)createNoti
{
(1)定义一个通知中心
NSNotificationCenter *nc=[NSNotificationCenter defaultCenter];
(2)添加通知中心的监听
[nc addObserver:self selector:@selector(showKeyBoardNotification:) name:UIKeyboardWillShowNotification object:nil];
[nc addObserver:self selector:@selector(hideKeyBoardNotification:) name:UIKeyboardWillHideNotification object:nil];
}
2、键盘将弹出的通知
- (void)showKeyBoardNotification:(NSNotification*)notification
{
(1)获取弹出键盘
NSDictionary *userInfo=notification.userInfo;
NSLog(@"键盘弹出:%@",userInfo);
NSTimeInterval duration=[[userInfo objectForKey:@"UIKeyboardAnimationDurationUserInfoKey"] floatValue];
(2)启动动画
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:duration];
UITextField *textField=(UITextField*)[self.view viewWithTag:11];
textField.frame=CGRectMake(20, 20, self.view.frame.size.width-40, 40);
[UIView commitAnimations];
}
3、键盘收起的监听方法
- (void)hideKeyBoardNotification:(NSNotification*)notification
{
(1)获取键盘弹出的数据
NSDictionary *userInfo=notification.userInfo;
NSLog(@"键盘收起:%@",userInfo);
NSTimeInterval duration=[[userInfo objectForKey:@"UIKeyboardAnimationDurationUserInfoKey"] floatValue];
(2)启动动画
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:duration];
UITextField *textField=(UITextField*)[self.view viewWithTag:11];
textField.frame=CGRectMake(20,100, self.view.frame.size.width-40, 40);
[UIView commitAnimations];
}
四、扩展:MVC模式
【注】iOS程序采用MVC设计模式为基础设计模式。即一个程序个构成,由模型,视图,控制器构成
1、模型(Model):非UI部分,数据的储存与管理
2、视图(View):UI部分,用于显示界面,与用户交互,包括接收触发事件,显示数据内容
3、控制器(Controller):本身不显示,用于管理视图,将视图部分,和模型部分链接起来。原则上视图不直接访问数据。
【注】使用MVC架构,视图不直接访问数据,可以便于修改,降低开发和维护成本。
五、例题:记录输入的内容,并保存重新显示到输入栏上