【Swift】自定義UIButton的圖片方向
給UIButton擴(kuò)展imagePosition方法用于設(shè)置圖片相對(duì)于文字的方向
//
// UIButton+ImgPositionExt.swift
// Comic
//
// Created by LRS on 2022/4/13.
//
import UIKit
extension UIButton {
//MARK: 定義button相對(duì)label的位置
enum ButtonImgPosition {
///圖片在上惧辈,文字在下,垂直居中對(duì)齊
case top
///圖片在下,文字在上吨悍,垂直居中對(duì)齊
case bottom
///圖片在左裆馒,文字在右健无,水平居中對(duì)齊
case left
///圖片在右回溺,文字在左啤贩,水平居中對(duì)齊
case right
}
/// - imagePosition 設(shè)置Button圖片的位置
/// - Parameters:
/// - style: 圖片位置
/// - spacing: 按鈕圖片與文字之間的間隔
func imagePosition(style:ButtonImgPosition,spacing:CGFloat){
self.layoutIfNeeded()
self.imageView?.contentMode = .scaleAspectFit
self.imageView?.sizeToFit()
//得到button的imageView和titleLabel的寬高
let imgvWidth = self.imageView?.frame.size.width
let imgvHeight = self.imageView?.frame.size.height
var labelWidth:CGFloat! = 0.0
var labelHeight:CGFloat! = 0.0
self.titleLabel?.sizeToFit()
labelWidth = self.titleLabel?.intrinsicContentSize.width
labelHeight = self.titleLabel?.intrinsicContentSize.height
//labelWidth = self.titleLabel?.size.width
//labelHeight = self.titleLabel?.size.height
if (style == .right || style == .left) {
if labelWidth > (self.width - spacing - imgvWidth!) {
labelWidth = (self.width - spacing - imgvWidth!)
self.titleLabel!.width = labelWidth
}
}else if (style == .top || style == .bottom){
if labelWidth > self.width {
labelWidth = self.width
self.titleLabel!.width = labelWidth
}
}
//初始化imageEdgeInsets和labelEdgeInsets
var imageEdgeInsets = UIEdgeInsets.zero
var labelEdgeInsets = UIEdgeInsets.zero
//根據(jù)style和space得到imageEdgeInsets和labelEdgeInsets的值
switch style {
case .top:
//順序 上 左 下 右
imageEdgeInsets = UIEdgeInsets(top: -labelHeight-spacing/2.0,
left: 0.0,
bottom: 0.0,
right: -labelWidth)
labelEdgeInsets = UIEdgeInsets(top: 0.0,
left: -imgvWidth!,
bottom: -imgvHeight!-spacing/2.0,
right: 0.0)
case .bottom:
imageEdgeInsets = UIEdgeInsets(top: 0.0,
left: 0.0,
bottom: -labelHeight!-spacing/2.0,
right: -labelWidth)
labelEdgeInsets = UIEdgeInsets(top: -imgvHeight!-spacing/2.0,
left: -imgvWidth!,
bottom: 0.0,
right: 0.0)
case .left:
imageEdgeInsets = UIEdgeInsets(top: 0.0,
left: -spacing/2.0,
bottom: 0.0,
right: spacing)
labelEdgeInsets = UIEdgeInsets(top: 0.0,
left: spacing/2.0,
bottom: 0.0,
right: -spacing/2)
case .right:
imageEdgeInsets = UIEdgeInsets(top: 0.0,
left: labelWidth+spacing/2,
bottom: 0.0,
right: -labelWidth-spacing/2)
labelEdgeInsets = UIEdgeInsets(top: 0.0,
left: -imgvWidth!-spacing/2,
bottom: 0.0,
right: imgvWidth!+spacing/2)
}
self.titleEdgeInsets = labelEdgeInsets
self.imageEdgeInsets = imageEdgeInsets
}
}
用法
//MARK: 定義button相對(duì)label的位置
enum ButtonImgPosition {
///圖片在上茫打,文字在下居触,垂直居中對(duì)齊
case top
///圖片在下,文字在上老赤,垂直居中對(duì)齊
case bottom
///圖片在左轮洋,文字在右,水平居中對(duì)齊
case left
///圖片在右抬旺,文字在左弊予,水平居中對(duì)齊
case right
}
說明:設(shè)置完button的size之后調(diào)用有效
let btn = UIButton(type: .custom).then { btn in
btn.frame = CGRect(x: 50.0, y: 100.0, width: 80.0, height: 80.0)
btn.backgroundColor = .red
//設(shè)置完button的size之后調(diào)用有效
btn.imagePosition(style: .bottom, spacing: 5.0)
btn.setImage(UIImage.imgColor(.green, CGSize(width: 40, height: 40)), for: .normal)
btn.setTitle("你好", for: .normal)
}
view.addSubview(btn)
btn.layoutIfNeeded()
btn.imagePosition(style: .bottom, spacing: 5.0)
效果
UIButton-圖片在文字下方