swiftSwift编程

Swift设置图片在Button的位置(上下左右及间距)

2018-07-16  本文已影响29人  我在鄱阳湖边

import UIKit

extension UIButton {

    /** 图片在左,标题在右 */

    func setIconInLeft() {

        setIconInLeftWithSpacing(0)

    }

    /** 图片在右,标题在左 */

    func setIconInRight() {

        setIconInRightWithSpacing(0)

    }

    /** 图片在上,标题在下 */

    func setIconInTop() {

        setIconInTopWithSpacing(0)

    }

    /** 图片在下,标题在上 */

    func setIconInBottom() {

        setIconInBottomWithSpacing(0)

    }

    //** 可以自定义图片和标题间的间隔 */

    func setIconInLeftWithSpacing(_ Spacing: CGFloat) {

        titleEdgeInsets = UIEdgeInsets()

        titleEdgeInsets.top = 0

        titleEdgeInsets.`left` = 0

        titleEdgeInsets.bottom = 0

        titleEdgeInsets.`right` = 0

        imageEdgeInsets = UIEdgeInsets()

        imageEdgeInsets.top = 0

        imageEdgeInsets.`left` = 0

        imageEdgeInsets.bottom = 0

        imageEdgeInsets.`right` = 0

    }

    func setIconInRightWithSpacing(_ Spacing: CGFloat) {

        let img_W: CGFloat? = imageView?.frame.size.width

        let tit_W: CGFloat? = titleLabel?.frame.size.width

        titleEdgeInsets = UIEdgeInsets()

        titleEdgeInsets.top = 0

        titleEdgeInsets.`left` = -(img_W + Spacing / 2)

        titleEdgeInsets.bottom = 0

        titleEdgeInsets.`right` = img_W + Spacing / 2

        imageEdgeInsets = UIEdgeInsets()

        imageEdgeInsets.top = 0

        imageEdgeInsets.`left` = tit_W + Spacing / 2

        imageEdgeInsets.bottom = 0

        imageEdgeInsets.`right` = -(tit_W + Spacing / 2)

    }

    func setIconInTopWithSpacing(_ Spacing: CGFloat) {

        let img_W: CGFloat? = imageView?.frame.size.width

        let img_H: CGFloat? = imageView?.frame.size.height

        let tit_W: CGFloat? = titleLabel?.frame.size.width

        let tit_H: CGFloat? = titleLabel?.frame.size.height

        titleEdgeInsets = UIEdgeInsets()

        titleEdgeInsets.top = tit_H / 2 + Spacing / 2

        titleEdgeInsets.`left` = -(img_W / 2)

        titleEdgeInsets.bottom = -(tit_H / 2 + Spacing / 2)

        titleEdgeInsets.`right` = img_W / 2

        imageEdgeInsets = UIEdgeInsets()

        imageEdgeInsets.top = -(img_H / 2 + Spacing / 2)

        imageEdgeInsets.`left` = tit_W / 2

        imageEdgeInsets.bottom = img_H / 2 + Spacing / 2

        imageEdgeInsets.`right` = -(tit_W / 2)

    }

    func setIconInBottomWithSpacing(_ Spacing: CGFloat) {

        let img_W: CGFloat? = imageView?.frame.size.width

        let img_H: CGFloat? = imageView?.frame.size.height

        let tit_W: CGFloat? = titleLabel?.frame.size.width

        let tit_H: CGFloat? = titleLabel?.frame.size.height

        titleEdgeInsets = UIEdgeInsets()

        titleEdgeInsets.top = -(tit_H / 2 + Spacing / 2)

        titleEdgeInsets.`left` = -(img_W / 2)

        titleEdgeInsets.bottom = tit_H / 2 + Spacing / 2

        titleEdgeInsets.`right` = img_W / 2

        imageEdgeInsets = UIEdgeInsets()

        imageEdgeInsets.top = img_H / 2 + Spacing / 2

        imageEdgeInsets.`left` = tit_W / 2

        imageEdgeInsets.bottom = -(img_H / 2 + Spacing / 2)

        imageEdgeInsets.`right` = -(tit_W / 2)

    }

}

上一篇 下一篇

猜你喜欢

热点阅读