UI初级 TextField

 1 //创建UITextFIeld对象,并设置frame
 2     UITextField *aTextField = [[UITextField alloc] init];
 3     
 4     CGFloat aTextFidldX = 40;
 5     CGFloat aTextFidldY = 44;
 6     CGFloat aTextFidldW = 150;
 7     CGFloat aTextFidldH = 40;
 8     
 9     aTextField.frame = CGRectMake(aTextFidldX, aTextFidldY, aTextFidldW, aTextFidldH);
10     //背景颜色
11     aTextField.backgroundColor = [UIColor cyanColor];
12     
13     //其他属性
14     aTextField.text = @"搜索";
15     aTextField.textColor = [UIColor redColor];
16     //居中
17     aTextField.textAlignment = NSTextAlignmentCenter;
18     //字体大小
19     aTextField.font = [UIFont systemFontOfSize:20];
20     //修饰边框
21     aTextField.borderStyle = UITextBorderStyleRoundedRect;
22     //占位符
23     aTextField.placeholder = @"请输入账号";
24     //编辑时不显示原有的字   干掉了"搜索"
25     aTextField.clearsOnBeginEditing = YES;
26     //后面的 x  删除整行tb
27     aTextField.clearButtonMode = UITextFieldViewModeAlways;
28     //是否安全输入  输入的字变点
29     aTextField.secureTextEntry = YES;
30     //设置键盘的样式
31 //  aTextField.keyboardType = UIKeyboardTypeNumbersAndPunctuation;
32     //设置ruturn Key的样式
33 //  aTextField.returnKeyType = UIReturnKeyNext;
34     
35 //  键盘回收三部曲
36     //1. 设置代理对象
37     aTextField.delegate = self;
38     //2.  (见 .h 文件)
39     
40     //3. 实现协议方法 (见下面)
41 
42     UITextField *bTextField = [[UITextField alloc] initWithFrame:CGRectMake(aTextFidldX, aTextFidldY + aTextFidldH + 20, aTextFidldW, aTextFidldH)];
43     
44     bTextField.backgroundColor = [UIColor greenColor];
45     
46     bTextField.borderStyle = UITextBorderStyleRoundedRect;
47     
48     bTextField.placeholder = @"请输入密码";
49     
50     bTextField.delegate = self;
51     
52     [self.window addSubview:aTextField];
53     [self.window addSubview:bTextField];
1 //  3. 实现协议方法
2 - (BOOL)textFieldShouldReturn:(UITextField *)textField
3 {
4     //第一响应者
5     [textField resignFirstResponder];
6     return YES;
7 }

有代理就得执行协议  我这里是在appdelegate里面写的 你可以在任何一个控制器中写

// 2. 遵守UITextFieldDelegate协议
@interface AppDelegate : UIResponder <UIApplicationDelegate,UITextFieldDelegate>

这个协议是在@interface后面写的,就<>里面的  别写错地方

当然最后这个代理和协议都是为了键盘回收,扩展知识,是在不懂可以不写

posted @ 2016-03-16 17:58  _小帅  阅读(200)  评论(0编辑  收藏  举报