UI控件笔记(四):UI之UITextField的属性
2016-05-17 10:07
561 查看
UITextField *firstTextField = [[UITextField
alloc] initWithFrame:CGRectMake(0,
20, 320,
44)];
[self.window
addSubview:firstTextField];
[firstTextField release];
一、UITextField的属性们
1、输入框的边界
firstTextField.borderStyle =
UITextBorderStyleNone;
2、提示文字
firstTextField.placeholder =
@"请在这里输入你的账号和密码";
3、自动更正英语单词
firstTextField.autocorrectionType =
UITextAutocorrectionTypeYes;
4、自动大写第一个字母
firstTextField.autocapitalizationType =
UITextAutocapitalizationTypeAllCharacters;
5、一键删除
firstTextField.clearButtonMode =
UITextFieldViewModeAlways;
6、密文
firstTextField.secureTextEntry =
YES;
7、左视图
UIView *leftView = [[UIView
alloc] initWithFrame:CGRectMake(0,
0, 30,
30)];
leftView.backgroundColor = [UIColor
redColor];
firstTextField.leftView = leftView;
firstTextField.leftViewMode =
UITextFieldViewModeAlways;
[leftView release];
8、右视图
UIView *rightView = [[UIView
alloc] initWithFrame:CGRectMake(0,
0, 30,
30)];
rightView.backgroundColor = [UIColor
greenColor];
firstTextField.rightView = rightView;
firstTextField.rightViewMode =
UITextFieldViewModeAlways;
[rightView release];
9、键盘类型
firstTextField.keyboardType =
UIKeyboardTypeDefault;
10、键盘右下角的returnkey的类型
firstTextField.returnKeyType =
UIReturnKeyDefault;
11、自定义键盘
UIView *customKeyBoard = [[UIView
alloc] initWithFrame:CGRectMake(0,
0, 320,
210)];
customKeyBoard.backgroundColor = [UIColor
lightGrayColor];
firstTextField.inputView = customKeyBoard;
[customKeyBoard release];
UIButton *labelBtn = [UIButton
buttonWithType:UIButtonTypeRoundedRect];
labelBtn.frame =
CGRectMake(10,
10, 300, 20);
[labelBtn setTitle:@"快点吧,我等的花儿都谢了"
forState:UIControlStateNormal];
[labelBtn addTarget:self
action:@selector(labelBtnDown:)
forControlEvents:UIControlEventTouchUpInside];
[customKeyBoard addSubview:labelBtn];
firstTextField.tag =
5000;
for(int i =
0;i<3;i++)
{
UIButton *btn = [UIButton
buttonWithType:UIButtonTypeCustom];
btn.frame =
CGRectMake((320-72)/4+i*(320-72)/4,
40, 24,
24);
[btn setImage:[UIImage
imageNamed:[NSString
stringWithFormat:@"%d.png",i]]
forState:UIControlStateNormal];
[btn addTarget:self
action:@selector(imageBtnDown:)
forControlEvents:UIControlEventTouchUpInside];
[customKeyBoard addSubview:btn];
btn.tag =
1000+i;
}
UIImageView *image = [[UIImageView
alloc] initWithFrame:CGRectMake(100,
100, 48,
48)];//自定义键盘上面的图片按钮点击显示用的
image.tag = 3000;
[self.window
addSubview:image];
[image release];
12、UITextField的代理
firstTextField.delegate =
self;
UILabel *label = [[UILabel
alloc] initWithFrame:CGRectMake(0,
150, 320,
44)];
[self.window
addSubview:label];
label.tag = 2000;
[label release];
二、按钮按钮按下去后触发的事件
1、图片按钮按下去后触发的方法
-(void)imageBtnDown:(UIButton*)btn
{
UIImageView *tempImageView = (UIImageView*)[self.window
viewWithTag:3000];
tempImageView.image = [UIImage
imageNamed:[NSString
stringWithFormat:@"%ld.png",btn.tag-1000]];
}
2、标签按钮按下去触发的事件
-(void)labelBtnDown:(UIButton*)btn
{
UITextField *tempTextField = (UITextField*)[self.window
viewWithTag:5000];
tempTextField.text = btn.titleLabel.text;
}
三、实现UITextField的代理方法,首先在AppDelegate.h文件中添加协议《UITextFieldDelegate》
#pragma mark
实现UITextField的代理方法
1、当textfield被点击,有了光标,开始能写内容,键盘弹出的时候,就会调用下面这个代理方法
-(void)textFieldDidBeginEditing:(UITextField *)textField
{
NSLog(@"开工了");
}
2、当键盘右下角的return键被点击的时候,会调用这个方法,这个代理方法只有iphone能用,ipad没有
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
NSLog(@"右下角被点了");
UILabel *lab = (UILabel*)[self.window
viewWithTag:2000];
lab.text = textField.text;
[textField resignFirstResponder];//移除这个textfield的第一响应,就是隐藏键盘
return
YES;
}
alloc] initWithFrame:CGRectMake(0,
20, 320,
44)];
[self.window
addSubview:firstTextField];
[firstTextField release];
一、UITextField的属性们
1、输入框的边界
firstTextField.borderStyle =
UITextBorderStyleNone;
2、提示文字
firstTextField.placeholder =
@"请在这里输入你的账号和密码";
3、自动更正英语单词
firstTextField.autocorrectionType =
UITextAutocorrectionTypeYes;
4、自动大写第一个字母
firstTextField.autocapitalizationType =
UITextAutocapitalizationTypeAllCharacters;
5、一键删除
firstTextField.clearButtonMode =
UITextFieldViewModeAlways;
6、密文
firstTextField.secureTextEntry =
YES;
7、左视图
UIView *leftView = [[UIView
alloc] initWithFrame:CGRectMake(0,
0, 30,
30)];
leftView.backgroundColor = [UIColor
redColor];
firstTextField.leftView = leftView;
firstTextField.leftViewMode =
UITextFieldViewModeAlways;
[leftView release];
8、右视图
UIView *rightView = [[UIView
alloc] initWithFrame:CGRectMake(0,
0, 30,
30)];
rightView.backgroundColor = [UIColor
greenColor];
firstTextField.rightView = rightView;
firstTextField.rightViewMode =
UITextFieldViewModeAlways;
[rightView release];
9、键盘类型
firstTextField.keyboardType =
UIKeyboardTypeDefault;
10、键盘右下角的returnkey的类型
firstTextField.returnKeyType =
UIReturnKeyDefault;
11、自定义键盘
UIView *customKeyBoard = [[UIView
alloc] initWithFrame:CGRectMake(0,
0, 320,
210)];
customKeyBoard.backgroundColor = [UIColor
lightGrayColor];
firstTextField.inputView = customKeyBoard;
[customKeyBoard release];
UIButton *labelBtn = [UIButton
buttonWithType:UIButtonTypeRoundedRect];
labelBtn.frame =
CGRectMake(10,
10, 300, 20);
[labelBtn setTitle:@"快点吧,我等的花儿都谢了"
forState:UIControlStateNormal];
[labelBtn addTarget:self
action:@selector(labelBtnDown:)
forControlEvents:UIControlEventTouchUpInside];
[customKeyBoard addSubview:labelBtn];
firstTextField.tag =
5000;
for(int i =
0;i<3;i++)
{
UIButton *btn = [UIButton
buttonWithType:UIButtonTypeCustom];
btn.frame =
CGRectMake((320-72)/4+i*(320-72)/4,
40, 24,
24);
[btn setImage:[UIImage
imageNamed:[NSString
stringWithFormat:@"%d.png",i]]
forState:UIControlStateNormal];
[btn addTarget:self
action:@selector(imageBtnDown:)
forControlEvents:UIControlEventTouchUpInside];
[customKeyBoard addSubview:btn];
btn.tag =
1000+i;
}
UIImageView *image = [[UIImageView
alloc] initWithFrame:CGRectMake(100,
100, 48,
48)];//自定义键盘上面的图片按钮点击显示用的
image.tag = 3000;
[self.window
addSubview:image];
[image release];
12、UITextField的代理
firstTextField.delegate =
self;
UILabel *label = [[UILabel
alloc] initWithFrame:CGRectMake(0,
150, 320,
44)];
[self.window
addSubview:label];
label.tag = 2000;
[label release];
二、按钮按钮按下去后触发的事件
1、图片按钮按下去后触发的方法
-(void)imageBtnDown:(UIButton*)btn
{
UIImageView *tempImageView = (UIImageView*)[self.window
viewWithTag:3000];
tempImageView.image = [UIImage
imageNamed:[NSString
stringWithFormat:@"%ld.png",btn.tag-1000]];
}
2、标签按钮按下去触发的事件
-(void)labelBtnDown:(UIButton*)btn
{
UITextField *tempTextField = (UITextField*)[self.window
viewWithTag:5000];
tempTextField.text = btn.titleLabel.text;
}
三、实现UITextField的代理方法,首先在AppDelegate.h文件中添加协议《UITextFieldDelegate》
#pragma mark
实现UITextField的代理方法
1、当textfield被点击,有了光标,开始能写内容,键盘弹出的时候,就会调用下面这个代理方法
-(void)textFieldDidBeginEditing:(UITextField *)textField
{
NSLog(@"开工了");
}
2、当键盘右下角的return键被点击的时候,会调用这个方法,这个代理方法只有iphone能用,ipad没有
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
NSLog(@"右下角被点了");
UILabel *lab = (UILabel*)[self.window
viewWithTag:2000];
lab.text = textField.text;
[textField resignFirstResponder];//移除这个textfield的第一响应,就是隐藏键盘
return
YES;
}
相关文章推荐
- KAlertController可更改外观的提示
- UI控件笔记(三):UI之UIButton的属性
- UIPopoverController
- easyui datagrid动态修改editor为combobox的options
- UI控件笔记(二):UI之UILabel和UIImageView
- LeetCode 187. Repeated DNA Sequences
- easyui formatter
- UIKit框架解读
- Jbuilder 代码格式
- android UI——自定义view(layout)重写方法的分析
- easyui清除dategrid所有数据(模拟本地加载)
- iOS开发UI之初识CALayer
- Android SDK requires Android Developer Toolkit version 23.0.0 or above
- UI控件笔记(一):UI之UIView的属性
- 网易视频云:新一代列式存储格式Parquet
- UITextField 的限制输入金额(可为小数的正确金额)
- UItextField
- 错误The request sent by the client was syntactically incorrect ()的解决
- Java集合类: Set、List、Map、Queue使用场景梳理
- getValues() 还是 getFieldValues() [转]