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()
- }