swift学习之UIButton

//

//  ViewController.swift

//  button

//

//  Created by su on 15/12/7.

//  Copyright © 2015年 tian. All rights reserved.

//

import UIKit

class ViewController: UIViewController {

    override func viewDidLoad() {

        super.viewDidLoad()

        //创建一个button

        let button:UIButton = UIButton(frame: CGRect(x: 110, y: 70, 100, height: 50))

        button.setTitleColor(UIColor.redColor(), forState: UIControlState.Normal)

        

        //设置不同状态的文字,依次是普通状态和按下状态

        button.setTitle("你点我呀!", forState: UIControlState.Normal)

        button.setTitle("你还真点啊", forState: UIControlState.Highlighted)

        //通过addTarget方法为按钮添加交互响应,依次为按下事件

        button.addTarget(self, action: "press:", forControlEvents: UIControlEvents.TouchUpInside)

        self.view.addSubview(button)

        

        //创建图形按钮

        let normalImage = UIImage(named: "btn1")

        let highLightedImage = UIImage(named: "btn2")

        let button2 = UIButton(frame: CGRect(x: 110, y: 200, 100, height: 30))

        

        button2.setImage(normalImage, forState: UIControlState.Normal)

        button2.setImage(highLightedImage, forState: UIControlState.Highlighted)

        self.view.addSubview(button2)

        

        //创建一个图片加文字的按钮

        

        let button3 = UIButton(frame: CGRect(x: 0, y: 250, 400, height: 30))

        button3.setImage(UIImage(named: "btn1"), forState:UIControlState.Normal)

        button3.titleLabel?.font = UIFont.systemFontOfSize(14)

        

        button3.imageView?.contentMode = UIViewContentMode.ScaleAspectFit

        button3.setTitleColor(UIColor.blackColor(), forState: UIControlState.Normal)

        button3.setTitle("fsdfdsfsdfsadf", forState: UIControlState.Normal)

        self.view.addSubview(button3)

        

        //从系统定义的按钮类型常见button

        let btn4:UIButton = UIButton(type: UIButtonType.Custom)

        btn4.setTitleColor(UIColor.blackColor(), forState: UIControlState.Normal)

        btn4.setTitle("12344444", forState: UIControlState.Normal)

        btn4.titleLabel!.font = UIFont.systemFontOfSize(14)

        btn4.frame = CGRect(x: 110, y: 300, 100, height: 100)

        self.view.addSubview(btn4)

        

        

        //创建部分圆角的按钮

        

        let btn5:UIButton = UIButton(type: UIButtonType.Custom)

        btn5.backgroundColor = UIColor.redColor()

        btn5.setTitleColor(UIColor.blackColor(), forState: UIControlState.Normal)

        btn5.setTitle("345555", forState: UIControlState.Normal)

        btn5.titleLabel!.font = UIFont.systemFontOfSize(14)

        btn5.frame = CGRect(x: 110, y: 400, 100, height: 100)

        self.view.addSubview(btn5)

//        UIRectCornerTopLeft     = 1 << 0,

//        UIRectCornerTopRight    = 1 << 1,

//        UIRectCornerBottomLeft  = 1 << 2,

//        UIRectCornerBottomRight = 1 << 3,

//        UIRectCornerAllCorners  = ~0UL

//        let beizer:UIBezierPath = UIBezierPath(roundedRect: btn5.bounds, byRoundingCorners: UIRectCorner.TopLeft|UIRectCorner.TopRight, cornerRadii: CGSize( 15, height: 15))

        let beizer:UIBezierPath = UIBezierPath(roundedRect: btn5.bounds, byRoundingCorners: [UIRectCorner.TopLeft,UIRectCorner.TopRight], cornerRadii: CGSize( 15, height: 15))

        let shape:CAShapeLayer = CAShapeLayer()

        shape.path = beizer.CGPath

        btn5.layer.mask = shape

        

        //创建border按钮

        let btn9:UIButton = UIButton(frame: CGRect(x: 50, y: 500, 100, height: 35))

        btn9.backgroundColor = UIColor.whiteColor()

        btn9.setTitle("边框按钮", forState: UIControlState.Normal)

        btn9.setTitleColor(UIColor.blackColor(), forState: UIControlState.Normal)

        btn9.layer.borderColor = UIColor.blackColor().CGColor

        btn9.layer.borderWidth = 1

        btn9.layer.cornerRadius = 5

        self.view.addSubview(btn9)

        

        

        

        

        

    }

    func press(sender:UIButton){

        print("你按下了按钮")

    }

    override func didReceiveMemoryWarning() {

        super.didReceiveMemoryWarning()

        // Dispose of any resources that can be recreated.

    }

}

原文地址:https://www.cnblogs.com/tian-sun/p/5026396.html