6.UITextField和UITableView自定义下拉列表框
7.使用UIPickerView在UITextField实现滚动选择
8.使用UIDatePicker在UITextField实现日期选择
一、键盘风格
UIKit框架支持8种风格键盘。
typedef enum { UIKeyboardTypeDefault, // 默认键盘:支持所有字符 UIKeyboardTypeASCIICapable, // 支持ASCII的默认键盘 UIKeyboardTypeNumbersAndPunctuation, // 标准电话键盘,支持+*#等符号 UIKeyboardTypeURL, // URL键盘,有.com按钮;只支持URL字符 UIKeyboardTypeNumberPad, //数字键盘 UIKeyboardTypePhonePad, // 电话键盘 UIKeyboardTypeNamePhonePad, // 电话键盘,也支持输入人名字 UIKeyboardTypeEmailAddress, // 用于输入电子邮件地址的键盘 } UIKeyboardType;
用法用例:textView.keyboardtype = UIKeyboardTypeNumberPad;
二、键盘外观
typedef enum { UIKeyboardAppearanceDefault, // 默认外观:浅灰色 UIKeyboardAppearanceAlert, //深灰/石墨色 } UIKeyboardAppearance;
用法用例:textView.keyboardAppearance=UIKeyboardAppearanceDefault;
三、回车键
typedef enum { UIReturnKeyDefault, //默认:灰色按钮,标有Return UIReturnKeyGo, //标有Go的蓝色按钮 UIReturnKeyGoogle, //标有Google的蓝色按钮,用于搜索 UIReturnKeyJoin, //标有Join的蓝色按钮 UIReturnKeyNext, //标有Next的蓝色按钮 UIReturnKeyRoute, //标有Route的蓝色按钮 UIReturnKeySearch, //标有Search的蓝色按钮 UIReturnKeySend, //标有Send的蓝色按钮 UIReturnKeyYahoo, //标有Yahoo!的蓝色按钮,用于搜索 UIReturnKeyDone, //标有Done的蓝色按钮 UIReturnKeyEmergencyCall, //紧急呼叫按钮 } UIReturnKeyType;
用法用例:textView.returnKeyType=UIReturnKeyGo;
四、自动大写
typedef enum { UITextAutocapitalizationTypeNone, //不自动大写 UITextAutocapitalizationTypeWords, //单词首字母大写 UITextAutocapitalizationTypeSentences, //句子首字母大写 UITextAutocapitalizationTypeAllCharacters, //所有字母大写 } UITextAutocapitalizationType;
用法用例:textField.autocapitalizationType = UITextAutocapitalizationTypeWords;
五、自动更正
typedef enum { UITextAutocorrectionTypeDefault,//默认 UITextAutocorrectionTypeNo,//不自动更正 UITextAutocorrectionTypeYes,//自动更正 } UITextAutocorrectionType;
用法用例:textField.autocorrectionType = UITextAutocorrectionTypeYes;
六、安全文本输入
textView.secureTextEntry=YES;
开启安全输入主要是用于密码或一些私人数据的输入,此时会禁用自动更正和自此缓存。
//点击回车键的回调
UITextFieldDelegate -(BOOL)textFieldShouldReturn:(UITextField *)textField{ return YES; }
//结束触摸 - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { //isExclusiveTouch一个布尔值来指示接收机处理触摸事件。 //没有触摸_textUser进入if内操作 if (![_textUser isExclusiveTouch]) { //resignFirstResponder取消第一响应者状态的。如果对textfield使用的话,那么调用这个方法,textfield的第一响应者状态就会取消,然后键盘就消失了。 [_textUser resignFirstResponder]; } }
//或者
//结束触摸 -(void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{ [self.view endEditing:YES];//收回键盘 }
//需要处理输入框被键盘挡住的.h文件,遵守UITextFieldDelegate协议
#import <UIKit/UIKit.h> #import "MyKeyboard.h" @interface Test2ViewController : UIViewController<MykeyBoardDelegate,UITextFieldDelegate> @end
//需要处理输入框被键盘挡住的.m文件,实现UITextFieldDelegate协议方法
//实现委托 _textUser.tag=1; _textUser.delegate=self; _textPwd.tag=2; _textPwd.delegate=self;
//选中开始编辑文本框内容
-(void)textFieldDidBeginEditing:(UITextField *)textField{ _textTag=textField.tag; //根据tag判断这个键盘是否是被遮住的键盘 if (_textTag==2) { //将界面上移避免文本框被键盘挡住 CGRect frame = self.view.frame; frame.origin.y -=200; frame.size.height +=200; self.view.frame = frame; } }
//结束编辑文本框内容
-(void)textFieldDidEndEditing:(UITextField *)textField{ //根据tag判断这个键盘是否是被遮住的键盘 if (_textTag==2) { CGRect frame=self.view.frame; frame.origin.y+=200; frame.size.height-=200; self.view.frame=frame; } }
//MyKeyboard.h文件(自定义键盘)
#import <UIKit/UIKit.h> @protocol MykeyBoardDelegate<NSObject> //文本框值输入委托 -(void)myKeyBoardInput:(NSString *)inputString; //删除文本框字委托 -(void)myKeyBoardBack; //隐藏键盘委托 -(void)myKeyBoardOK; @end @interface MyKeyboard : UIView @property (nonatomic,weak) id<MykeyBoardDelegate> delegate;//声明一个委托变量 @end
// // MyKeyboard.m // GGTabBarDemo // // Created by Vie on 15/7/23. // Copyright (c) 2015年 Vie. All rights reserved. // #import "MyKeyboard.h" @implementation MyKeyboard -(id)initWithFrame:(CGRect)frame{ self=[super initWithFrame:frame]; if (self) { self.bounds=CGRectMake(0, 0, frame.size.width, frame.size.height); //第一横排键盘 UIButton *button600=[[UIButton alloc] initWithFrame:CGRectMake(frame.size.width*0.005, 0, frame.size.width*0.19, frame.size.height*0.24)]; [button600.layer setCornerRadius:2.0]; [button600.layer setBorderWidth:0.5]; [button600 setBackgroundColor:[UIColor whiteColor]]; [button600 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; [button600 setTitle:@"600" forState:UIControlStateNormal]; [button600 addTarget:self action:@selector(inputAction:) forControlEvents:UIControlEventTouchUpInside]; [self addSubview:button600]; UIButton *button1=[[UIButton alloc] initWithFrame:CGRectMake(frame.size.width*0.2, 0, frame.size.width*0.19, frame.size.height*0.24)]; [button1.layer setCornerRadius:2.0]; [button1.layer setBorderWidth:0.5]; [button1 setBackgroundColor:[UIColor whiteColor]]; [button1 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; [button1 setTitle:@"1" forState:UIControlStateNormal]; [button1 addTarget:self action:@selector(inputAction:) forControlEvents:UIControlEventTouchUpInside]; [self addSubview:button1]; UIButton *button2=[[UIButton alloc] initWithFrame:CGRectMake(frame.size.width*0.4, 0, frame.size.width*0.19, frame.size.height*0.24)]; [button2.layer setCornerRadius:2.0]; [button2.layer setBorderWidth:0.5]; [button2 setBackgroundColor:[UIColor whiteColor]]; [button2 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; [button2 setTitle:@"2" forState:UIControlStateNormal]; [button2 addTarget:self action:@selector(inputAction:) forControlEvents:UIControlEventTouchUpInside]; [self addSubview:button2]; UIButton *button3=[[UIButton alloc] initWithFrame:CGRectMake(frame.size.width*0.6, 0, frame.size.width*0.19, frame.size.height*0.24)]; [button3.layer setCornerRadius:2.0]; [button3.layer setBorderWidth:0.5]; [button3 setBackgroundColor:[UIColor whiteColor]]; [button3 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; [button3 setTitle:@"3" forState:UIControlStateNormal]; [button3 addTarget:self action:@selector(inputAction:) forControlEvents:UIControlEventTouchUpInside]; [self addSubview:button3]; UIButton *buttonX=[[UIButton alloc] initWithFrame:CGRectMake(frame.size.width*0.8, 0, frame.size.width*0.19, frame.size.height*0.24)]; [buttonX.layer setCornerRadius:2.0]; [buttonX.layer setBorderWidth:0.5]; [buttonX setBackgroundColor:[UIColor whiteColor]]; [buttonX setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; [buttonX setTitle:@"X" forState:UIControlStateNormal]; [buttonX addTarget:self action:@selector(backAction:) forControlEvents:UIControlEventTouchUpInside]; [self addSubview:buttonX]; //第二横排键盘 UIButton *button601=[[UIButton alloc] initWithFrame:CGRectMake(frame.size.width*0.005, frame.size.height*0.25, frame.size.width*0.19, frame.size.height*0.24)]; [button601.layer setCornerRadius:2.0]; [button601.layer setBorderWidth:0.5]; [button601 setBackgroundColor:[UIColor whiteColor]]; [button601 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; [button601 setTitle:@"601" forState:UIControlStateNormal]; [button601 addTarget:self action:@selector(inputAction:) forControlEvents:UIControlEventTouchUpInside]; [self addSubview:button601]; UIButton *button4=[[UIButton alloc] initWithFrame:CGRectMake(frame.size.width*0.2, frame.size.height*0.25, frame.size.width*0.19, frame.size.height*0.24)]; [button4.layer setCornerRadius:2.0]; [button4.layer setBorderWidth:0.5]; [button4 setBackgroundColor:[UIColor whiteColor]]; [button4 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; [button4 setTitle:@"4" forState:UIControlStateNormal]; [button4 addTarget:self action:@selector(inputAction:) forControlEvents:UIControlEventTouchUpInside]; [self addSubview:button4]; UIButton *button5=[[UIButton alloc] initWithFrame:CGRectMake(frame.size.width*0.4, frame.size.height*0.25, frame.size.width*0.19, frame.size.height*0.24)]; [button5.layer setCornerRadius:2.0]; [button5.layer setBorderWidth:0.5]; [button5 setBackgroundColor:[UIColor whiteColor]]; [button5 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; [button5 setTitle:@"5" forState:UIControlStateNormal]; [button5 addTarget:self action:@selector(inputAction:) forControlEvents:UIControlEventTouchUpInside]; [self addSubview:button5]; UIButton *button6=[[UIButton alloc] initWithFrame:CGRectMake(frame.size.width*0.6, frame.size.height*0.25, frame.size.width*0.19, frame.size.height*0.24)]; [button6.layer setCornerRadius:2.0]; [button6.layer setBorderWidth:0.5]; [button6 setBackgroundColor:[UIColor whiteColor]]; [button6 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; [button6 setTitle:@"6" forState:UIControlStateNormal]; [button6 addTarget:self action:@selector(inputAction:) forControlEvents:UIControlEventTouchUpInside]; [self addSubview:button6]; UIButton *button002=[[UIButton alloc] initWithFrame:CGRectMake(frame.size.width*0.8, frame.size.height*0.25, frame.size.width*0.19, frame.size.height*0.24)]; [button002.layer setCornerRadius:2.0]; [button002.layer setBorderWidth:0.5]; [button002 setBackgroundColor:[UIColor whiteColor]]; [button002 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; [button002 setTitle:@"002" forState:UIControlStateNormal]; [button002 addTarget:self action:@selector(inputAction:) forControlEvents:UIControlEventTouchUpInside]; [self addSubview:button002]; //第三横排键盘 UIButton *button000=[[UIButton alloc] initWithFrame:CGRectMake(frame.size.width*0.005, frame.size.height*0.5, frame.size.width*0.19, frame.size.height*0.24)]; [button000.layer setCornerRadius:2.0]; [button000.layer setBorderWidth:0.5]; [button000 setBackgroundColor:[UIColor whiteColor]]; [button000 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; [button000 setTitle:@"000" forState:UIControlStateNormal]; [button000 addTarget:self action:@selector(inputAction:) forControlEvents:UIControlEventTouchUpInside]; [self addSubview:button000]; UIButton *button7=[[UIButton alloc] initWithFrame:CGRectMake(frame.size.width*0.2, frame.size.height*0.5, frame.size.width*0.19, frame.size.height*0.24)]; [button7.layer setCornerRadius:2.0]; [button7.layer setBorderWidth:0.5]; [button7 setBackgroundColor:[UIColor whiteColor]]; [button7 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; [button7 setTitle:@"7" forState:UIControlStateNormal]; [button7 addTarget:self action:@selector(inputAction:) forControlEvents:UIControlEventTouchUpInside]; [self addSubview:button7]; UIButton *button8=[[UIButton alloc] initWithFrame:CGRectMake(frame.size.width*0.4, frame.size.height*0.5, frame.size.width*0.19, frame.size.height*0.24)]; [button8.layer setCornerRadius:2.0]; [button8.layer setBorderWidth:0.5]; [button8 setBackgroundColor:[UIColor whiteColor]]; [button8 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; [button8 setTitle:@"8" forState:UIControlStateNormal]; [button8 addTarget:self action:@selector(inputAction:) forControlEvents:UIControlEventTouchUpInside]; [self addSubview:button8]; UIButton *button9=[[UIButton alloc] initWithFrame:CGRectMake(frame.size.width*0.6, frame.size.height*0.5, frame.size.width*0.19, frame.size.height*0.24)]; [button9.layer setCornerRadius:2.0]; [button9.layer setBorderWidth:0.5]; [button9 setBackgroundColor:[UIColor whiteColor]]; [button9 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; [button9 setTitle:@"9" forState:UIControlStateNormal]; [button9 addTarget:self action:@selector(inputAction:) forControlEvents:UIControlEventTouchUpInside]; [self addSubview:button9]; UIButton *button300=[[UIButton alloc] initWithFrame:CGRectMake(frame.size.width*0.8, frame.size.height*0.5, frame.size.width*0.19, frame.size.height*0.24)]; [button300.layer setCornerRadius:2.0]; [button300.layer setBorderWidth:0.5]; [button300 setBackgroundColor:[UIColor whiteColor]]; [button300 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; [button300 setTitle:@"300" forState:UIControlStateNormal]; [button300 addTarget:self action:@selector(inputAction:) forControlEvents:UIControlEventTouchUpInside]; [self addSubview:button300]; //第四横排键盘 UIButton *button0=[[UIButton alloc] initWithFrame:CGRectMake(frame.size.width*0.2, frame.size.height*0.75, frame.size.width*0.19, frame.size.height*0.24)]; [button0.layer setCornerRadius:2.0]; [button0.layer setBorderWidth:0.5]; [button0 setBackgroundColor:[UIColor whiteColor]]; [button0 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; [button0 setTitle:@"0" forState:UIControlStateNormal]; [button0 addTarget:self action:@selector(inputAction:) forControlEvents:UIControlEventTouchUpInside]; [self addSubview:button0]; UIButton *buttonPoint=[[UIButton alloc] initWithFrame:CGRectMake(frame.size.width*0.4, frame.size.height*0.75, frame.size.width*0.19, frame.size.height*0.24)]; [buttonPoint.layer setCornerRadius:2.0]; [buttonPoint.layer setBorderWidth:0.5]; [buttonPoint setBackgroundColor:[UIColor whiteColor]]; [buttonPoint setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; [buttonPoint setTitle:@"." forState:UIControlStateNormal]; [buttonPoint addTarget:self action:@selector(inputAction:) forControlEvents:UIControlEventTouchUpInside]; [self addSubview:buttonPoint]; UIButton *buttonOK=[[UIButton alloc] initWithFrame:CGRectMake(frame.size.width*0.6, frame.size.height*0.75, frame.size.width*0.19, frame.size.height*0.24)]; [buttonOK.layer setCornerRadius:2.0]; [buttonOK.layer setBorderWidth:0.5]; [buttonOK setBackgroundColor:[UIColor whiteColor]]; [buttonOK setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; [buttonOK setTitle:@"确定" forState:UIControlStateNormal]; [buttonOK addTarget:self action:@selector(backAction:) forControlEvents:UIControlEventTouchUpInside]; [self addSubview:buttonOK]; } return self; } -(void)inputAction:(UIButton *)sender{ [self.delegate myKeyBoardInput:[sender currentTitle]]; } -(void)backAction:(UIButton *)sender{ if ([[sender currentTitle] isEqualToString:@"确定"]) { [self.delegate myKeyBoardOK]; }else if ([[sender currentTitle] isEqualToString:@"X"]){ [self.delegate myKeyBoardBack]; } } @end
//Test2ViewController.h文件,需要用到自定义软件盘的文本框
#import <UIKit/UIKit.h> #import "MyKeyboard.h" @interface Test2ViewController : UIViewController<MykeyBoardDelegate,UITextFieldDelegate> @end
//Test2ViewController.m文件,需要用到自定义软件盘的文本框
// // Test2ViewController.m // // // Created by Vie on 15/7/22. // // #import "Test2ViewController.h" #import "MyKeyboard.h" @interface Test2ViewController () @property (nonatomic,strong) UITextField *textUser,*textPwd; @property (nonatomic) int textTag; @end @implementation Test2ViewController @synthesize textUser=_textUser,textPwd=_textPwd,textTag=_textTag; - (void)viewDidLoad { [super viewDidLoad]; [self.view setBackgroundColor:[UIColor whiteColor]]; self.parentViewController.navigationItem.title = @"2"; //设置标题 self.navigationItem.title = @"登陆"; //修改导航栏背景色 [self.navigationController.navigationBar setBarTintColor:[UIColor colorWithRed:30/255.0 green:144/255.0 blue:255/255.0 alpha:0.5]]; //设置标题颜色字体大小 [self.navigationController.navigationBar setTitleTextAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:19],NSForegroundColorAttributeName:[UIColor colorWithRed:255/255.0 green:255/255.0 blue:255/255.0 alpha:1]}]; //修改导航栏背景色 [self.navigationController.navigationBar setBarTintColor:[UIColor colorWithRed:30/255.0 green:144/255.0 blue:255/255.0 alpha:0.5]]; //下一个视图的返回按钮文件会改变为下面设置的值 UIBarButtonItem *returnButtonItem = [[UIBarButtonItem alloc] init]; returnButtonItem.title = @""; self.navigationItem.backBarButtonItem = returnButtonItem; //用户名输入文本框 _textUser=[[UITextField alloc] initWithFrame:CGRectMake(self.view.frame.size.width*0.1, self.view.frame.size.height*0.2, self.view.frame.size.width*0.8, self.view.frame.size.height*0.08)]; [_textUser setBackgroundColor:[UIColor whiteColor]]; [_textUser.layer setCornerRadius:5.0]; [_textUser setPlaceholder:@"用户名"]; [_textUser.layer setBorderWidth:1]; //初始化自定义键盘 MyKeyboard *myKboard=[[MyKeyboard alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height*0.3)]; //引用自定义键盘 _textUser.inputView=myKboard; //委托 myKboard.delegate=self; _textUser.tag=1; _textUser.delegate=self; [self.view addSubview:_textUser]; //密码输入文本框 _textPwd=[[UITextField alloc] initWithFrame:CGRectMake(self.view.frame.size.width*0.1, self.view.frame.size.height*0.8, self.view.frame.size.width*0.8, self.view.frame.size.height*0.08)]; [_textPwd setBackgroundColor:[UIColor whiteColor]]; [_textPwd.layer setCornerRadius:5.0]; [_textPwd setPlaceholder:@"密码"]; [_textPwd.layer setBorderWidth:1]; _textPwd.inputView=myKboard; _textPwd.tag=2; _textPwd.delegate=self; //响应文本框选中更改 [self.view addSubview:_textPwd]; } //选中开始编辑文本框内容 -(void)textFieldDidBeginEditing:(UITextField *)textField{ _textTag=textField.tag; //根据tag判断这个键盘是否是被遮住的键盘 if (_textTag==2) { //将界面上移避免文本框被键盘挡住 CGRect frame = self.view.frame; frame.origin.y -=200; frame.size.height +=200; self.view.frame = frame; } } //结束编辑文本框内容 -(void)textFieldDidEndEditing:(UITextField *)textField{ //根据tag判断这个键盘是否是被遮住的键盘 if (_textTag==2) { CGRect frame=self.view.frame; frame.origin.y+=200; frame.size.height-=200; self.view.frame=frame; } } //键盘输入实现 -(void)myKeyBoardInput:(NSString *)inputString{ //点击的是用户名文本框时 if (_textTag==1) { self.textUser.text=[self.textUser.text stringByAppendingString:inputString]; }else if(_textTag==2){ self.textPwd.text=[self.textPwd.text stringByAppendingString:inputString]; } } //键盘删除实现 -(void)myKeyBoardBack{ if (_textTag==1) { if (self.textUser.text.length>0) { self.textUser.text=[self.textUser.text substringToIndex:self.textUser.text.length-1]; } }else if (_textTag==2){ if (self.textPwd.text.length>0) { self.textPwd.text=[self.textPwd.text substringToIndex:self.textPwd.text.length-1]; } } } //隐藏键盘实现 -(void)myKeyBoardOK{ if (_textTag==1) { [_textUser resignFirstResponder]; }else if (_textTag==2){ [_textPwd resignFirstResponder]; } } //结束触摸 - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { //isExclusiveTouch一个布尔值来指示接收机处理触摸事件。 //没有触摸_textUser进入if内操作 if (![_textUser isExclusiveTouch]) { //resignFirstResponder取消第一响应者状态的。如果对textfield使用的话,那么调用这个方法,textfield的第一响应者状态就会取消,然后键盘就消失了。 [_textUser resignFirstResponder]; } if (![_textPwd isExclusiveTouch]) { //resignFirstResponder取消第一响应者状态的。如果对textfield使用的话,那么调用这个方法,textfield的第一响应者状态就会取消,然后键盘就消失了。 [_textPwd resignFirstResponder]; } } @end
//效果
//键盘弹出
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyBoardChanged:) name:UIKeyboardWillChangeFrameNotification object:nil];
//键盘消失
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyBoardWillHide:) name:UIKeyboardWillHideNotification object:nil];
//监听键盘弹出方法 -(void)keyBoardChanged:(NSNotification *)notification{ //UIKeyboardFrameEndUserInfoKey 将要变化的大小 CGRect keyBoardRect=[notification.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue]; keyBoardHeight=keyBoardRect.size.height;//获取弹出键盘高度 //键盘弹出时间 NSTimeInterval time=[notification.userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue]; //下面可以做文本输入框坐标改变动画,与键盘弹出耗费时间一致(体验比较好) [UIView animateWithDuration:time animations:^{ //self.view.frame=changeFrame; }]; } //监听键盘消失方法 -(void)keyBoardWillHide:(NSNotification *)notification{ //键盘弹出时间 NSTimeInterval time=[notification.userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue]; //下面可以做文本输入框坐标改变动画,与键盘弹出耗费时间一致(体验比较好) [UIView animateWithDuration:time animations:^{ //self.view.frame=changeFrame; }]; }
UITextField和UITableView自定义下拉列表框
//效果图:
//DownboxViewController.h文件
#import <UIKit/UIKit.h> #import "CommboxView.h" @interface DownboxViewController : UIViewController @property (nonatomic,strong) CommboxView *cbView; @
//DownboxViewController.m文件
#import "DownboxViewController.h" @interface DownboxViewController () @end @implementation DownboxViewController - (void)viewDidLoad { [super viewDidLoad]; [self.view setBackgroundColor:[UIColor whiteColor]]; //1、创建视图 CGFloat yyX = 40; CGFloat yyY = 40; CGFloat yyW = self.view.frame.size.width-80; CGFloat yyH = 100; CommboxView *yy = [[CommboxView alloc] initWithFrame:CGRectMake(yyX, yyY, yyW, yyH)]; //2、完成布局 [self.view addSubview:yy]; //3、赋值成员变量 self.cbView = yy; self.cbView.textField.placeholder=@"请选择"; //设置数据 self.cbView.tbArray=[[NSArray alloc] initWithObjects:@"风继续吹",@"真的爱你",@"透明的你",@"爱的太迟",@"Dear friends",@"永远不回头", nil]; } //结束触摸时,隐藏下拉列表框 -(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{ //isExclusiveTouch一个布尔值来指示接收机处理触摸事件。 //没有触摸_textUser进入if内操作 if (![self.cbView.textField isExclusiveTouch]) { //resignFirstResponder取消第一响应者状态的。如果对textfield使用的话,那么调用这个方法,textfield的第一响应者状态就会取消,然后键盘就消失了。 [self.cbView.textField resignFirstResponder]; self.cbView.tbView.hidden=YES; //设置右边view self.cbView.textField.rightView=[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"login_textfield_more@2x.png"]]; } } @end
//CommboxView.h文件
#import <UIKit/UIKit.h> @interface CommboxView : UIView<UITableViewDataSource,UITableViewDelegate,UITextFieldDelegate> @property (nonatomic,strong) UITableView *tbView;//用作显示下拉列表框 @property (nonatomic,strong) NSArray *tbArray;//用作下拉列表框的数据 @property (nonatomic,strong) UITextField *textField;//文本框 @end
//CommboxView.m文件
#import "CommboxView.h" @implementation CommboxView - (instancetype)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if ( self ) { //1、创建视图 CGFloat yyX = 0; CGFloat yyY = 0; CGFloat yyW = frame.size.width; CGFloat yyH = frame.size.height/2.5; UITextField *uiText=[[UITextField alloc] initWithFrame:CGRectMake(yyX, yyY, yyW, yyH)]; [uiText.layer setCornerRadius:5.0 ]; [uiText.layer setBorderWidth:1.0]; //完成布局 [self addSubview:uiText]; uiText.delegate=self; //设置右边view uiText.rightView=[[UIImageView alloc] initWithImage:[UIImageimageNamed:@"login_textfield_more@2x.png"]]; //设置右边样式总是显示 uiText.rightViewMode = UITextFieldViewModeAlways; //赋值成员变量 self.textField=uiText; //创建视图 UITableView *uiTable=[[UITableView alloc] initWithFrame:CGRectMake(yyX, yyH, yyW, frame.size.height-yyY)]; uiTable.backgroundColor=[UIColor grayColor]; //分割线颜色 uiTable.separatorColor=[UIColor lightGrayColor]; [uiTable.layer setCornerRadius:5.0]; //完成布局 [self addSubview:uiTable]; uiTable.dataSource=self; uiTable.delegate=self; uiTable.hidden=YES; //赋值成员变量 self.tbView=uiTable; } return self; } //选中单元格操作 -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath{ self.textField.text=[_tbArray objectAtIndex:indexPath.row]; [self.textField resignFirstResponder]; self.tbView.hidden=YES; //设置右边view self.textField.rightView=[[UIImageView alloc] initWithImage:[UIImageimageNamed:@"login_textfield_more@2x.png"]]; } //开始编辑文本框时将下拉列表显示 -(void)textFieldDidBeginEditing:(UITextField *)textField{ //取消第一响应者状态,可以到达无焦点,不弹出键盘的效果 [self.textField resignFirstResponder]; self.tbView.hidden=NO; //设置右边view self.textField.rightView=[[UIImageView alloc] initWithImage:[UIImageimageNamed:@"login_textfield_more_flip@2x.png"]]; } //设置单元格宽度 -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath*)indexPath{ return self.textField.frame.size.height*0.8; } //每个分区多少行 -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ return self.tbArray.count; } //设置单元格 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ static NSString *cellDo=@"cellDo"; UITableViewCell *cell=(UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:cellDo]; if (!cell) { cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:cellDo]; } [[cell textLabel] setText:[_tbArray objectAtIndex:indexPath.row]]; cell.backgroundColor=[UIColor grayColor]; return cell; } @end
使用UIPickerView在UITextField实现滚动选择
// PickerTestViewController.m文件
// // PickerTestViewController.m // // // Created by Vie on 15/10/12. // // #import "PickerTestViewController.h" @interface PickerTestViewController ()<UIPickerViewDelegate,UIPickerViewDataSource> @property(nonatomic,strong)UIPickerView *pickerView; @property(nonatomic,strong)UITextField *txField; @property(nonatomic,strong)NSMutableArray *yearArray,*monthArray,*dayArray; @end @implementation PickerTestViewController - (void)viewDidLoad { [super viewDidLoad]; [self.view setBackgroundColor:[UIColor whiteColor]]; self.yearArray=[[NSMutableArray alloc] initWithObjects:@"2013",@"2014",@"2015", nil]; self.monthArray=[[NSMutableArray alloc] initWithObjects:@"1",@"2",@"3",@"4",@"5", nil]; self.dayArray=[[NSMutableArray alloc] initWithObjects:@"1",@"2",@"3",@"4",@"5",@"6",@"7",@"8", nil]; self.pickerView=[[UIPickerView alloc] init]; self.pickerView.dataSource=self; self.pickerView.delegate=self; self.txField=[[UITextField alloc] initWithFrame:CGRectMake(self.view.frame.size.width*0.1f, 100, self.view.frame.size.width*0.8f, 44)]; self.txField.layer.borderWidth=1.0f; self.txField.layer.cornerRadius=5.0f; [self.view addSubview:self.txField]; //设置输入框的弹出视图为UIPickerView self.txField.inputView=self.pickerView; } //用户选中某个row时,对应改变文本 -(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{ [self setTxFieldText]; } //结束文本框触摸时隐藏UIPickerView -(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{ if (![self.txField isExclusiveTouch]) { [self.txField resignFirstResponder]; [self setTxFieldText]; } } //将文本框的内容设置为滚动表格选中值 -(void)setTxFieldText{ self.txField.text=[NSString stringWithFormat:@"%@-%@-%@",[self.yearArrayobjectAtIndex:[self.pickerView selectedRowInComponent:0]],[self.monthArrayobjectAtIndex:[self.pickerView selectedRowInComponent:1]],[self.dayArrayobjectAtIndex:[self.pickerView selectedRowInComponent:2]]]; } //指定UIPickerView上的文本 -(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{ NSString *str; if (component==0) { str=[self.yearArray objectAtIndex:row]; }else if (component==1){ str=[self.monthArray objectAtIndex:row]; }else{ str=[self.dayArray objectAtIndex:row]; } return str; } //返回每个轮最大的行数。 -(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{ if (component==0) { return self.yearArray.count; }else if (component==1){ return self.monthArray.count; }else{ return self.dayArray.count; } } //返回一个整数,表示列数 -(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{ return 3; } @end
使用UIDatePicker在UITextField实现日期选择
//DatePickerViewController.h文件
// // DatePickerViewController.h // // // Created by Vie on 15/10/12. // // #import <UIKit/UIKit.h> @interface DatePickerViewController : UIViewController @end
//DatePickerViewController.m文件
// // DatePickerViewController.m // // // Created by Vie on 15/10/12. // // #import "DatePickerViewController.h" @interface DatePickerViewController () @property(nonatomic,strong)UITextField *txField; @property(nonatomic,strong)UIDatePicker *datePicker; @end @implementation DatePickerViewController - (void)viewDidLoad { [super viewDidLoad]; [self.view setBackgroundColor:[UIColor whiteColor]]; self.txField=[[UITextField alloc] initWithFrame:CGRectMake(self.view.frame.size.width*0.1f, 100, self.view.frame.size.width*0.8f, 44)]; self.txField.layer.borderWidth=1.0f; self.txField.layer.cornerRadius=5.0f; self.txField.placeholder=@"选择日期"; [self.view addSubview:self.txField]; self.datePicker=[[UIDatePicker alloc] init]; //设置日期选取器模式 self.datePicker.datePickerMode=UIDatePickerModeDate; //默认根据手机本地设置来显示为中文还是其他语言 NSLocale *locale=[[NSLocale alloc] initWithLocaleIdentifier:@"zh_CN"];//设置为中文显示 self.datePicker.locale=locale; //设置日期范围 NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; [dateFormatter setDateFormat: @"yyyy-MM-dd"]; NSDate *minDate= [dateFormatter dateFromString:@"1970-01-01"]; NSDate *maxDate=[dateFormatter dateFromString:@"2030-12-31"]; self.datePicker.minimumDate=minDate; self.datePicker.maximumDate=maxDate; //设置响应事件 [self.datePicker addTarget:self action:@selector(pickerValueChanged) forControlEvents:UIControlEventValueChanged]; [self.txField setInputView:self.datePicker]; } //结束文本框触摸时隐藏UIPickerView -(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{ if (![self.txField isExclusiveTouch]) { [self.txField resignFirstResponder]; [self setTxFieldText]; } } //选择器改变时,对应改变文本框内容 -(void)pickerValueChanged{ [self setTxFieldText]; } //设置文本框内容 -(void)setTxFieldText{ NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; [dateFormatter setDateFormat: @"yyyy-MM-dd"]; NSString *dateString=[dateFormatter stringFromDate:self.datePicker.date]; self.txField.text=dateString; } @end
//图片验证码输入 self.imgLable=[[UILabel alloc] initWithFrame:CGRectMake(0, 0, telLableWidth, telLableHeight)]; self.imgLable.text=@"验证码"; self.imgLable.textColor=[UIColor blackColor]; self.imgLable.numberOfLines=0; self.imgLable.textAlignment=NSTextAlignmentCenter; self.imgLable.font=[UIFont fontWithName:@"Arial" size:18.0f]; UIView *imgBgView=[[UIView alloc] initWithFrame:CGRectMake(0, 0, 95, 40)]; self.codeImgView=[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 80, 38)]; self.codeImgView.center=imgBgView.center; self.codeImgView.backgroundColor=[UIColor grayColor]; [imgBgView addSubview:self.codeImgView]; float imgCodeFielY=statusHeight+navHeight+30+self.telLable.frame.size.height; self.imgCodeField=[[UITextField alloc] initWithFrame:CGRectMake(0, imgCodeFielY, telFieldWidth, telLableHeight)]; self.imgCodeField.placeholder=@"输入图片验证码"; [self.imgCodeField setBorderStyle:UITextBorderStyleRoundedRect]; //设置输入框左边视图 self.imgCodeField.leftView=self.imgLable; self.imgCodeField.leftViewMode=UITextFieldViewModeAlways; //设置输入框右边视图 self.imgCodeField.rightView=imgBgView; self.imgCodeField.rightViewMode=UITextFieldViewModeAlways; self.imgCodeField.tag=102; self.imgCodeField.delegate=self; self.imgCodeField.keyboardType=UIKeyboardTypeEmailAddress; [self.view addSubview:self.imgCodeField];
实现带文本输入的UIAlertView弹窗
UIAlertView *alerView=[[UIAlertView alloc] initWithTitle:@"请输入账户密码" message:nil delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil]; [alerView setAlertViewStyle:UIAlertViewStyleLoginAndPasswordInput]; UITextField *accountField=[alerView textFieldAtIndex:0]; accountField.placeholder=@"请输入账户"; UITextField *pwdField=[alerView textFieldAtIndex:1]; pwdField.placeholder=@"请输入密码"; [alerView show]; -(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{ NSLog(@"账号:%@;密码:%@",[alertView textFieldAtIndex:0].text,[alertView textFieldAtIndex:1].text); }