import UIKit
extension UIButton {
? ? /** 圖片在左,標(biāo)題在右 */
? ? func setIconInLeft() {
? ? ? ? setIconInLeftWithSpacing(0)
? ? }
? ? /** 圖片在右,標(biāo)題在左 */
? ? func setIconInRight() {
? ? ? ? setIconInRightWithSpacing(0)
? ? }
? ? /** 圖片在上,標(biāo)題在下 */
? ? func setIconInTop() {
? ? ? ? setIconInTopWithSpacing(0)
? ? }
? ? /** 圖片在下,標(biāo)題在上 */
? ? func setIconInBottom() {
? ? ? ? setIconInBottomWithSpacing(0)
? ? }
? ? //** 可以自定義圖片和標(biāo)題間的間隔 */
? ? 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)
? ? }
}