Rider For Unreal 常用快捷键整理

Mybatis--万能Map---映射器(mappers)-缓存-LOG4J--MyBatis 分页插件--使用注解开发---#{} 与 ${}区别--Lombok--多对一对多处理动态 SQL

  返回  

[Swift]UIButton设置图片在文字左、右、上、下侧

2021/7/20 14:36:26 浏览:
import UIKit
import EZSwiftExtensions

class OptimalCell: UITableViewCell {

    /// 分享
    let btn_share : LLButton = LLButton()

    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        self.selectionStyle = .none
        initUI()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    fileprivate func initUI() {
        self.contentView.addSubview(btn_down)
        btn_down.addTapGesture { [weak self](_) in
            guard let this = self else{return}
            this.click_down()
        }
        btn_down.backgroundColor = UIColor(hexString:"#F50E34")!
        btn_down.setImage(UIImage(named: "down.png"), for: .normal)
        btn_down.setTitle("下载", for: .normal)
        btn_down.setTitleColor(.white, for: .normal)
        btn_down.titleLabel?.font = UIFont.systemFont(ofSize: CGFloat(12))
        btn_down.imageAlignment = .left
        btn_down.spaceBetweenTitleAndImage = 4
        btn_down.setCornerRadius(radius: 12)
        btn_down.snp.makeConstraints { (make) in
            make.right.equalToSuperview().offset(-25)
            make.top.equalToSuperview().offset(25)
            make.width.equalTo(60)
            make.height.equalTo(24)
        }
        
        self.contentView.addSubview(btn_share)
        btn_share.addTapGesture { [weak self](_) in
            guard let this = self else{return}
            this.click_share()
        }
        
        btn_share.addBorder(width: 1, color: UIColor(hexString: "#F50E34")!)
        btn_share.backgroundColor = UIColor(hexString:"#ffffff")!
        btn_share.setImage(UIImage(named: "fx.png"), for: .normal)
        btn_share.setTitleColor(UIColor(hexString: "#F50E34"), for: .normal)
        btn_share.titleLabel?.font = UIFont.systemFont(ofSize: CGFloat(12))
        btn_share.imageAlignment = .left
        btn_share.spaceBetweenTitleAndImage = 4
        btn_share.setCornerRadius(radius: 12)
        btn_share.snp.makeConstraints { (make) in
            make.right.equalTo(btn_down.snp_left).offset(-10)
            make.centerY.equalTo(btn_down)
            make.width.equalTo(64)
            make.height.equalTo(24)
        }
    }
}

LLButton

import UIKit

enum LLImageAlignment: NSInteger {
    case left = 0
    case top
    case bottom
    case right
}

class LLButton: UIButton {

    var imageAlignment: LLImageAlignment = .left
    var spaceBetweenTitleAndImage: CGFloat = 0
    
    override func layoutSubviews() {
        super.layoutSubviews()
        
        let space: CGFloat = self.spaceBetweenTitleAndImage
        
        let titleW: CGFloat = self.titleLabel?.bounds.width ?? 0
        let titleH: CGFloat = self.titleLabel?.bounds.height ?? 0
        
        let imageW: CGFloat = self.imageView?.bounds.width ?? 0
        let imageH: CGFloat = self.imageView?.bounds.height ?? 0
        
        let btnCenterX: CGFloat = self.bounds.width / 2
        let imageCenterX: CGFloat = btnCenterX - titleW / 2
        let titleCenterX = btnCenterX + imageW / 2
        
        switch self.imageAlignment {
        case .top:
            self.titleEdgeInsets = UIEdgeInsets(top: imageH / 2 + space / 2, left: -(titleCenterX - btnCenterX), bottom: -(imageH/2 + space/2), right: titleCenterX-btnCenterX)
            self.imageEdgeInsets = UIEdgeInsets(top: -(titleH / 2 + space / 2), left: btnCenterX - imageCenterX, bottom: titleH / 2 + space / 2, right: -(btnCenterX - imageCenterX));
        case .left:
            self.titleEdgeInsets = UIEdgeInsets(top: 0, left: space / 2, bottom: 0, right: -space / 2);
            self.imageEdgeInsets = UIEdgeInsets(top: 0, left: -space / 2, bottom: 0, right: space);
        case .bottom:
            self.titleEdgeInsets = UIEdgeInsets(top: -(imageH / 2 + space / 2), left: -(titleCenterX - btnCenterX), bottom: imageH / 2 + space / 2, right: titleCenterX - btnCenterX);
            self.imageEdgeInsets = UIEdgeInsets(top: titleH / 2 + space / 2, left: btnCenterX - imageCenterX,bottom: -(titleH / 2 + space / 2), right: -(btnCenterX - imageCenterX));
        case .right:
            self.titleEdgeInsets = UIEdgeInsets(top: 0, left: -(imageW + space / 2), bottom: 0, right: imageW + space / 2);
            self.imageEdgeInsets = UIEdgeInsets(top: 0, left: titleW + space / 2, bottom: 0, right: -(titleW + space / 2));
        }
    }

}

示意图

 

联系我们

如果您对我们的服务有兴趣,请及时和我们联系!

服务热线:18288888888
座机:18288888888
传真:
邮箱:888888@qq.com
地址:郑州市文化路红专路93号