Swift学习第六枪-UIButton和UILable

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

今天就学习这两个控件。
代码地址:这里写链接内容

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值