【swift_2】swift之三大控件 UILabel、UITextField、UIButton 的使用

来源:互联网 发布:matlab无法读取txt数据 编辑:程序博客网 时间:2024/06/11 11:00

Demo:链接: http://download.csdn.net/download/riven_wn/9401930


一.UILabel 

[objc] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. //创建label  
  2.     func creatLabel()  
  3.     {  
  4.         let label = UILabel(frame: CGRect(x: 20.0,y: 100.0,width: 80.0,height: 44.0))  
  5.         //设置属性  
  6.         label.text = "用户名:"  
  7.         label.backgroundColor = .yellowColor()  
  8.         label.textAlignment = .Left  
  9.         label.font = .systemFontOfSize(18.0)  
  10.         label.textColor = UIColor.blueColor()  
  11.         self.view.addSubview(label)  
  12.     }  


二.UITextField

[objc] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. //创建textField  
  2.     func creatTextField()  
  3.     {  
  4.         let textField = UITextField(frame: CGRect(x: 100.0,y: 100.0,width: 200.0,height: 44.0))  
  5.           
  6.         /* 边框样式 */  
  7.         textField.borderStyle = UITextBorderStyle.RoundedRect //圆角矩形边框  
  8. //        textField.borderStyle = UITextBorderStyle.None //无边框  
  9. //        textField.borderStyle = UITextBorderStyle.Line //直线边框  
  10. //        textField.borderStyle = UITextBorderStyle.Bezel //边线 + 阴影  
  11.           
  12.         /* 提示文字 */  
  13.         textField.placeholder = "请输入用户名"  
  14.         textField.adjustsFontSizeToFitWidth=true  //当文字超出文本框宽度时,自动调整文字大小  
  15.         textField.minimumFontSize = 14                  //最小可缩小的字号  
  16.           
  17.         /** 水平对齐 **/  
  18.         //        textField.textAlignment = .Right  //水平右对齐  
  19.         //        textField.textAlignment = .Center //水平居中对齐  
  20.         textField.textAlignment = .Left     //水平左对齐  
  21.           
  22.         /** 垂直对齐 **/  
  23.         //        textField.contentVerticalAlignment = .Top     //垂直向上对齐  
  24.         //        textField.contentVerticalAlignment = .Center  //垂直居中对齐  
  25.         textField.contentVerticalAlignment = .Bottom    //垂直向下对齐  
  26.           
  27.         /* 清除按钮(输入框内右侧小叉)*/  
  28.         textField.clearButtonMode=UITextFieldViewMode.WhileEditing  //编辑时出现清除按钮  
  29.         //        textField.clearButtonMode=UITextFieldViewMode.UnlessEditing  //编辑时不出现,编辑后才出现清除按钮  
  30.         //        textField.clearButtonMode=UITextFieldViewMode.Always  //一直显示清除按钮  
  31.           
  32.         textField.becomeFirstResponder()//使文本框在界面打开时就获取焦点,并弹出输入键盘  
  33.           
  34.         /* 设置键盘return键的样式 */  
  35.         textField.returnKeyType = UIReturnKeyType.Done //表示完成输入  
  36.         //        textField.returnKeyType = UIReturnKeyType.Go //表示完成输入,同时会跳到另一页  
  37.         //        textField.returnKeyType = UIReturnKeyType.Search //表示搜索  
  38.         //        textField.returnKeyType = UIReturnKeyType.Join //表示注册用户或添加数据  
  39.         //        textField.returnKeyType = UIReturnKeyType.Next //表示继续下一步  
  40.         //        textField.returnKeyType = UIReturnKeyType.Send //表示发送  
  41.           
  42.         textField.delegate = self //注意看上边引用UITextFieldDelegate的方法是用“,”分隔,而不是用"<>"  
  43.           
  44.         self.view.addSubview(textField)  
  45.     }  
[objc] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. //点击return键,收起键盘的函数  
  2. func textFieldShouldReturn(textField:UITextField) -> Bool  
  3. {  
  4.     //收起键盘  
  5.     textField.resignFirstResponder()  
  6.     //打印出文本框中的值  
  7.     println(textField.text)  
  8.     return true;  
  9. }  

三.UIButton

[objc] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. //创建button  
  2. func creatButton()  
  3. {  
  4.     let button = UIButton(frame:CGRectMake(10020080,44))  
  5.     //设置title  
  6.     button.setTitle("点我",forState: UIControlState.Normal)  
  7.     //设置button 的title color  
  8.     button.setTitleColor(UIColor.redColor(),forState: UIControlState.Normal)  
  9.     //设置button的显示图片  
  10.     var backImage = UIImage(named:"icon114.png")  
  11.     button.setImage(backImage, forState : UIControlState.Normal)  
  12.     //给button添加事件  
  13.     button.addTarget(self,action:"buttonActions:",forControlEvents:UIControlEvents.TouchUpInside)  
  14.     //给button设置背景颜色  
  15.     var whitColor = UIColor(red:1.0,green:1.0,blue:1.0,alpha:1.0)  
  16.     button.backgroundColor = whitColor  
  17.     self.view.addSubview(button)  
  18.       
  19. }  
  20.   
  21. //按钮的点击事件  
  22. func buttonActions(sender:UIButton!)  
  23. {  
  24.     let alert = UIAlertView(title: "提示", message"我被点击了,嘻嘻嘻", delegate: nil, cancelButtonTitle"真开心")  
  25.     alert .show()  
  26. }  
0 0
原创粉丝点击