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后面写的,就<>里面的 别写错地方
当然最后这个代理和协议都是为了键盘回收,扩展知识,是在不懂可以不写
相关文章推荐
- POJ 2299 Ultra-QuickSort
- UI初级 Label
- 使用Fragment 实现动态UI
- Elasticsearch 评分score计算中的Boost 和 queryNorm
- UI入门 纯代码 第一节 UIWindow, UIView
- Starting MySQL. ERROR! The server quit without updating PID file
- 详解 UIView 的 Tint Color 属性
- iOS UITextField不显示光标
- 关于Android studio项目sdk1\build-tools\23.0.1\zipalign.exe'' finished with non-zero exit value 1问题
- android UI设计时须要注意遵循的设计原则
- pt-query-digest怎么分析慢查询日志分析数据
- requirejs笔记
- 异常收集之:navicatdesignquery.sql.bak 系统找不到指定路径
- 菜鸟成长记-UITableView
- Google 发布 Bottom Navigation design guidelines
- UI设计常用的图像格式及特点太原UI设计师郭文军分享给你
- 开源libusb驱动的libwdi驱动安装API库和zadig.exe安装UI应用程序的编译和调试
- UIKit性能调优实战讲解
- UIScrollView控件实现图片轮播
- 0024-pg数据库的sequence问题