【swift_2】swift之三大控件 UILabel、UITextField、UIButton 的使用
来源:互联网 发布:matlab无法读取txt数据 编辑:程序博客网 时间:2024/06/11 11:00
Demo:链接: http://download.csdn.net/download/riven_wn/9401930
一.UILabel
- //创建label
- func creatLabel()
- {
- let label = UILabel(frame: CGRect(x: 20.0,y: 100.0,width: 80.0,height: 44.0))
- //设置属性
- label.text = "用户名:"
- label.backgroundColor = .yellowColor()
- label.textAlignment = .Left
- label.font = .systemFontOfSize(18.0)
- label.textColor = UIColor.blueColor()
- self.view.addSubview(label)
- }
二.UITextField
- //创建textField
- func creatTextField()
- {
- let textField = UITextField(frame: CGRect(x: 100.0,y: 100.0,width: 200.0,height: 44.0))
- /* 边框样式 */
- textField.borderStyle = UITextBorderStyle.RoundedRect //圆角矩形边框
- // textField.borderStyle = UITextBorderStyle.None //无边框
- // textField.borderStyle = UITextBorderStyle.Line //直线边框
- // textField.borderStyle = UITextBorderStyle.Bezel //边线 + 阴影
- /* 提示文字 */
- textField.placeholder = "请输入用户名"
- textField.adjustsFontSizeToFitWidth=true //当文字超出文本框宽度时,自动调整文字大小
- textField.minimumFontSize = 14 //最小可缩小的字号
- /** 水平对齐 **/
- // textField.textAlignment = .Right //水平右对齐
- // textField.textAlignment = .Center //水平居中对齐
- textField.textAlignment = .Left //水平左对齐
- /** 垂直对齐 **/
- // textField.contentVerticalAlignment = .Top //垂直向上对齐
- // textField.contentVerticalAlignment = .Center //垂直居中对齐
- textField.contentVerticalAlignment = .Bottom //垂直向下对齐
- /* 清除按钮(输入框内右侧小叉)*/
- textField.clearButtonMode=UITextFieldViewMode.WhileEditing //编辑时出现清除按钮
- // textField.clearButtonMode=UITextFieldViewMode.UnlessEditing //编辑时不出现,编辑后才出现清除按钮
- // textField.clearButtonMode=UITextFieldViewMode.Always //一直显示清除按钮
- textField.becomeFirstResponder()//使文本框在界面打开时就获取焦点,并弹出输入键盘
- /* 设置键盘return键的样式 */
- textField.returnKeyType = UIReturnKeyType.Done //表示完成输入
- // textField.returnKeyType = UIReturnKeyType.Go //表示完成输入,同时会跳到另一页
- // textField.returnKeyType = UIReturnKeyType.Search //表示搜索
- // textField.returnKeyType = UIReturnKeyType.Join //表示注册用户或添加数据
- // textField.returnKeyType = UIReturnKeyType.Next //表示继续下一步
- // textField.returnKeyType = UIReturnKeyType.Send //表示发送
- textField.delegate = self //注意看上边引用UITextFieldDelegate的方法是用“,”分隔,而不是用"<>"
- self.view.addSubview(textField)
- }
- //点击return键,收起键盘的函数
- func textFieldShouldReturn(textField:UITextField) -> Bool
- {
- //收起键盘
- textField.resignFirstResponder()
- //打印出文本框中的值
- println(textField.text)
- return true;
- }
三.UIButton
- //创建button
- func creatButton()
- {
- let button = UIButton(frame:CGRectMake(100, 200, 80,44))
- //设置title
- button.setTitle("点我",forState: UIControlState.Normal)
- //设置button 的title color
- button.setTitleColor(UIColor.redColor(),forState: UIControlState.Normal)
- //设置button的显示图片
- var backImage = UIImage(named:"icon114.png")
- button.setImage(backImage, forState : UIControlState.Normal)
- //给button添加事件
- button.addTarget(self,action:"buttonActions:",forControlEvents:UIControlEvents.TouchUpInside)
- //给button设置背景颜色
- var whitColor = UIColor(red:1.0,green:1.0,blue:1.0,alpha:1.0)
- button.backgroundColor = whitColor
- self.view.addSubview(button)
- }
- //按钮的点击事件
- func buttonActions(sender:UIButton!)
- {
- let alert = UIAlertView(title: "提示", message: "我被点击了,嘻嘻嘻", delegate: nil, cancelButtonTitle: "真开心")
- alert .show()
- }
0 0
- 【swift_2】swift之三大控件 UILabel、UITextField、UIButton 的使用
- 【swift_2】swift之三大控件 UILabel、UITextField、UIButton 的使用
- UILabel,UIButton,UITextField的简单使用
- UILabel、UITextField、UIButton、UIImageView控件
- Swift UIButton、UILabel、UITextField的创建与用法
- 基础控件之UILabel、UIButton、UITextField、UIAlertView、 UIImageView
- UI基本控件-UILabel-UITextField-UIButton-UIAlertView
- UI-基本控件( UILabel UITextField UIImageView UIButton)
- swift IOS 视图控制器 UIButton UILabel 控件使用
- swift IOS 视图控制器 UIButton UILabel 控件使用
- swift IOS 视图控制器 UIButton UILabel 控件使用
- iOSDay21之UILabel, UITextField, UIButton, UIImageView
- UILabel,UITextField和UIButton使用简示
- UILabel,UITextField 以及UIButton的应用
- UIButton,UIImageView,UILabel,UITextField
- UITextField、UILabel、UIButton
- UILabel, UITextField, UIButton
- UILabel,UITextField,UIButton
- canvas实现环形进度条
- MySQL数据库相关操作
- linux下安装codeblocks
- getopt(分析命令行参数)
- 注册账号第一发:关于activity生命周期的几点认识
- 【swift_2】swift之三大控件 UILabel、UITextField、UIButton 的使用
- Android Studio : Gradle
- pthread_self 、pthread_exit
- Android最佳性能实践(四)——布局优化技巧
- Core Data 版本迁移经验
- 解决MySql Error Code: 2006 – MySQL 服务器已离线 错误
- RatingBar 修改 颜色 大小 图片
- 实现水平滑动,并且动态添加数据
- 田忌赛马