UIButton+icon

import UIKit

extension UIButton {
    
    static func icon(
        imageName: String,
        tintColor: UIColor? = nil,
        size: CGSize = CGSize(width: 44, height: 44),
        cornerRadius: CGFloat = 0,
        backgroundColor: UIColor? = nil
    ) -> UIButton {
        let image = UIImage.named(imageName)
        let button = iconButton(image:image,tintColor:tintColor,size:size,cornerRadius:cornerRadius,backgroundColor:backgroundColor)
        return button
    }
    
    static func iconButton(
        image: UIImage,
        tintColor: UIColor? = nil,
        size: CGSize = CGSize(width: 44, height: 44),
        cornerRadius: CGFloat = 0,
        backgroundColor: UIColor? = nil
    ) -> UIButton {
        let button = UIButton(type: .custom)
        button.setImage(image, for: .normal)
        
        if let tintColor = tintColor {
            button.tintColor = tintColor
            button.setImage(image.withRenderingMode(.alwaysTemplate), for: .normal)
        }
        button.frame = CGRect(origin: .zero, size: size)
        button.widthAnchor.constraint(equalToConstant: size.width).isActive = true
        button.heightAnchor.constraint(equalToConstant: size.height).isActive = true
        if cornerRadius > 0 {
            button.layer.cornerRadius = cornerRadius
            button.clipsToBounds = true
        }
        if let backgroundColor = backgroundColor {
            button.backgroundColor = backgroundColor
        }
        return button
    }
    
    static func iconButton(
        systemName: String,
        tintColor: UIColor? = nil,
        size: CGSize = CGSize(width: 44, height: 44),
        cornerRadius: CGFloat = 0,
        backgroundColor: UIColor? = nil,
        pointSize: CGFloat = 17,
        weight: UIImage.SymbolWeight = .regular
    ) -> UIButton {
        let configuration = UIImage.SymbolConfiguration(pointSize: pointSize, weight: weight)
        var image = UIImage(systemName: systemName, withConfiguration: configuration)
        if image == nil {
            print("❌无法加载系统图标: \(systemName)")
            image = UIImage(systemName: "ellipsis", withConfiguration: configuration)
        }
        return iconButton(
            image: image!,
            tintColor: tintColor ?? .systemBlue,
            size: size,
            cornerRadius: cornerRadius,
            backgroundColor: backgroundColor
        )
    }
    
    func addTarget_u(_ target: Any?, action: Selector) {
        addTarget(target, action: action, for: .touchUpInside)
    }

    func removeTarget_u(_ target: Any?, action: Selector) {
        removeTarget(target, action: action, for: .touchUpInside)
    }
}

extension UIImage {
    
    static func named(_ named:String?) -> UIImage {
        var image = UIImage(named:named ?? "")
        if image == nil {
            print("❌找不到\(named ?? "").png文件")
            image = UIImage(systemName: "ellipsis")
        }
        return image!
    }

}

extension UIImageView {
    
    convenience init(_ imageName:String,cornerRadius:CGFloat?=nil,borderColor:UIColor?=nil,borderWidth:CGFloat?=nil,contentMode:UIView.ContentMode?=nil) {
        self.init()
        self.image = UIImage.named(imageName)
        self.contentMode = contentMode ?? UIView.ContentMode.scaleAspectFill
        self.makeCornerRadius(cornerRadius, borderColor: borderColor, borderWidth: borderWidth)
    }
    
}

发表评论