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

UI初级 TextField

2016-03-16 17:58 281 查看
//创建UITextFIeld对象,并设置frame
UITextField *aTextField = [[UITextField alloc] init];

CGFloat aTextFidldX = 40;
CGFloat aTextFidldY = 44;
CGFloat aTextFidldW = 150;
CGFloat aTextFidldH = 40;

aTextField.frame = CGRectMake(aTextFidldX, aTextFidldY, aTextFidldW, aTextFidldH);
//背景颜色
aTextField.backgroundColor = [UIColor cyanColor];

//其他属性
aTextField.text = @"搜索";
aTextField.textColor = [UIColor redColor];
//居中
aTextField.textAlignment = NSTextAlignmentCenter;
//字体大小
aTextField.font = [UIFont systemFontOfSize:20];
//修饰边框
aTextField.borderStyle = UITextBorderStyleRoundedRect;
//占位符
aTextField.placeholder = @"请输入账号";
//编辑时不显示原有的字   干掉了"搜索"
aTextField.clearsOnBeginEditing = YES;
//后面的 x  删除整行tb
aTextField.clearButtonMode = UITextFieldViewModeAlways;
//是否安全输入  输入的字变点
aTextField.secureTextEntry = YES;
//设置键盘的样式
//  aTextField.keyboardType = UIKeyboardTypeNumbersAndPunctuation;
//设置ruturn Key的样式
//  aTextField.returnKeyType = UIReturnKeyNext;

//  键盘回收三部曲
//1. 设置代理对象
aTextField.delegate = self;
//2.  (见 .h 文件)

//3. 实现协议方法 (见下面)

UITextField *bTextField = [[UITextField alloc] initWithFrame:CGRectMake(aTextFidldX, aTextFidldY + aTextFidldH + 20, aTextFidldW, aTextFidldH)];

bTextField.backgroundColor = [UIColor greenColor];

bTextField.borderStyle = UITextBorderStyleRoundedRect;

bTextField.placeholder = @"请输入密码";

bTextField.delegate = self;

[self.window addSubview:aTextField];
[self.window addSubview:bTextField];


//  3. 实现协议方法
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
//第一响应者
[textField resignFirstResponder];
return YES;
}


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

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


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

当然最后这个代理和协议都是为了键盘回收,扩展知识,是在不懂可以不写
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: