您的位置:首页 > 产品设计 > UI/UE

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;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: