Swift学习第六枪-UIButton和UILable
2016-05-21 17:49
351 查看
UIButton和UILable的学习
从今天开始学习基本控件,先从按钮和标签开始。1.UIButton相关属性
构造方法(UIButton(type:UIButtonType.InfoDark))位置和大小 :frame
背景颜色 :backgroundColor
前景颜色 : tintColor
文字 : setTitle
添加点击事件 :func addTarget(target: AnyObject?, action: Selector, forControlEvents controlEvents: UIControlEvents)
标记 : tag
按钮的圆角 : layer.masksToBounds = true
圆角的半径 :layer.cornerRadius
边框的颜色:ayer.borderColo
边框的宽度 :layer.borderWidth
2.代码的实现
let btn1 = UIButton(type:UIButtonType.InfoDark) btn1.frame = CGRectMake(130, 80, 40, 40) let btn2 = UIButton(type:UIButtonType.RoundedRect) //设置按钮的位置和大小 btn2.frame = CGRectMake(80, 180, 150, 44) //设置按钮的背景颜色 btn2.backgroundColor = UIColor.purpleColor() //设置按钮的前景颜色 btn2.tintColor = UIColor.yellowColor() //设置按钮的文字 btn2.setTitle("Press ON", forState: .Normal) //设置按钮的点击事件 btn2.addTarget(self, action: #selector(UIBUttonViewController.buttonTag(_:)), forControlEvents: UIControlEvents.TouchUpInside) //为按钮设置标记 btn2.tag = 20 let btn3 = UIButton(type:UIButtonType.RoundedRect) btn3.backgroundColor = UIColor.brownColor() btn3.tintColor = UIColor.whiteColor() btn3.setTitle("Press Off", forState: .Normal) btn3.frame = CGRectMake(80, 280, 150, 44) btn3.layer.masksToBounds = true //设置按钮的圆角半径为10 btn3.layer.cornerRadius = 5 //设置按钮的边框宽度为4 btn3.layer.borderWidth = 1 //设置按钮的边框颜色 btn3.layer.borderColor = UIColor.lightGrayColor().CGColor self.view.addSubview(btn1) self.view.addSubview(btn2) self.view.addSubview(btn3) /** *按钮的事件处理 **/ func buttonTag(btn:UIButton) { let alter = UIAlertController(title: "Information",message: "Button Event",preferredStyle: UIAlertControllerStyle.Alert) let oKAction = UIAlertAction(title: "OK",style: UIAlertActionStyle.Default,handler: nil) alter.addAction(oKAction) self.presentViewController(alter, animated: true, completion: nil) }
3 .UILable的相关属性
内容 :text字体:font
文字的阴影颜色 :shadowColor
文字的阴影在横向和纵向的偏移距离:shadowOffset
文字的对其的方式:textAlignment
标签文字的颜色 :textColor
标签背景颜色:backgroundColor
4.UILable的代码实现
let rect = CGRectMake(20, 440, 280, 80) let lable = UILabel(frame: rect) lable.text = "Hello Lable" let font = UIFont(name: "宋体",size: 12) lable.font = font //设置文字的阴影颜色 lable.shadowColor = UIColor.lightGrayColor() //设置标签文字的阴影在横向和纵向的偏移距离 lable.shadowOffset = CGSizeMake(2,2) //设置文字的对其的方式 lable.textAlignment = NSTextAlignment.Center //设置标签文字的颜色 lable.textColor = UIColor.purpleColor()//紫色 //设置标签的背景颜色为黄色 lable.backgroundColor = UIColor.yellowColor()
今天就学习这两个控件。
代码地址:这里写链接内容
相关文章推荐
- swift+tableview+数据展示
- swift+tableview+数据展示
- iOS开发实战:使用Swift语言自定义Pull To Refresh控件
- swift中一个类定义一个属性必须初始化,如果不想初始化可以加?
- swift纯代码创建view
- swift闭包
- swift 协议
- swift getter setter方法
- swift uitableview
- swift遍历数组
- swift构造方法
- swift懒加载
- swift循环
- swift数组
- swift字典
- swift字符串转为oc
- swift字符串
- Swift-析构过程(Deinitialization)(十三)
- Swift 可选值
- IOS-swift 动画04 CALayer属性动画