Swift 实现UIButton图片与文字多种布局

UIButton的文字图片布局实现以下效果

调用方式如下:

```

    var btn1 = GJVerButton()

    var btn2 = GJVerButton()

    var btn3 = GJVerButton()

    var btn4 = GJVerButton()

    override func viewDidLoad() {

        super.viewDidLoad()

        for i in 0..<4{

            let button = GJVerButton()

            button.backgroundColor = .orange

            button.setTitle("微信",for: .normal)

            button.setTitleColor(.black,for: .normal)

            button.setImage(.init(named:"wechat"),for: .normal)

            button.contentEdgeInsets = UIEdgeInsets(top:20,left:20,bottom:20,right:20)

            switch i {

            case 0:

                self.btn1 = button

            case 1:

                self.btn2 = button

            case 2:

                self.btn3 = button

            default:

                self.btn4 = button

            }

        }

        view.addSubview(btn1)

        view.addSubview(btn2)

        view.addSubview(btn3)

        view.addSubview(btn4)

        let verh = 60

        btn1.snp.makeConstraints{ make in

            make.centerX.equalTo(view.snp.centerX)

            make.top.equalTo(view.snp.top).offset(100)

        }

        btn2.snp.makeConstraints{ make in

            make.centerX.equalTo(view.snp.centerX)

            make.top.equalTo(btn1.snp.bottom).offset(verh)

        }

        btn3.snp.makeConstraints{ make in

            make.centerX.equalTo(view.snp.centerX)

            make.top.equalTo(btn2.snp.bottom).offset(verh)

        }

        btn4.snp.makeConstraints{ make in

            make.centerX.equalTo(view.snp.centerX)

            make.top.equalTo(btn3.snp.bottom).offset(verh)

        }

        btn1.imagePosition(style: .top, spacing: 10)

        btn2.imagePosition(style: .bottom, spacing: 10)

        btn3.imagePosition(style: .left, spacing: 10)

        btn4.imagePosition(style: .right, spacing: 10)

    }

```


GJVerButton.swift代码实现

```

class GJVerButton: UIButton {

    enum RGButtonImagePosition {

    case top          //图片在上,文字在下,垂直居中对齐

    case bottom      //图片在下,文字在上,垂直居中对齐

    case left        //图片在左,文字在右,水平居中对齐

    case right        //图片在右,文字在左,水平居中对齐

    }

    func imagePosition(style: RGButtonImagePosition, spacing: CGFloat) {

    let imageWidth = self.imageView?.frame.size.width

    let imageHeight = self.imageView?.frame.size.height

    var labelWidth: CGFloat! = 0.0

    var labelHeight: CGFloat! = 0.0

    labelWidth = self.titleLabel?.intrinsicContentSize.width

    labelHeight = self.titleLabel?.intrinsicContentSize.height

    var imageEdgeInsets =  UIEdgeInsets.zero

    var labelEdgeInsets = UIEdgeInsets.zero

    switch style {

    case.top:

    imageEdgeInsets = UIEdgeInsets(top: -labelHeight-spacing/2,left:0,bottom:0,right: -labelWidth)

    labelEdgeInsets = UIEdgeInsets(top:0,left: -imageWidth!,bottom: -imageHeight!-spacing/2,right:0)

    break;

    case.left:

    imageEdgeInsets = UIEdgeInsets(top:0,left: -spacing/2,bottom:0,right: spacing)

    labelEdgeInsets = UIEdgeInsets(top:0,left: spacing/2,bottom:0,right: -spacing/2)

    break;

    case.bottom:

    imageEdgeInsets = UIEdgeInsets(top:0,left:0,bottom: -labelHeight!-spacing/2,right: -labelWidth)

    labelEdgeInsets = UIEdgeInsets(top: -imageHeight!-spacing/2,left: -imageWidth!,bottom:0,right:0)

    break;

    case.right:

    imageEdgeInsets = UIEdgeInsets(top:0,left: labelWidth+spacing/2,bottom:0,right: -labelWidth-spacing/2)

    labelEdgeInsets = UIEdgeInsets(top:0,left: -imageWidth!-spacing/2,bottom:0,right: imageWidth!+spacing/2)

    break;

    }

    self.titleEdgeInsets = labelEdgeInsets

    self.imageEdgeInsets = imageEdgeInsets

    }

}

```

你可能感兴趣的:(Swift 实现UIButton图片与文字多种布局)