119 lines
3.6 KiB
Swift
119 lines
3.6 KiB
Swift
|
|
//
|
||
|
|
// CLIconLabel.swift
|
||
|
|
// Crush
|
||
|
|
//
|
||
|
|
// Created by Leon on 2025/7/22.
|
||
|
|
//
|
||
|
|
|
||
|
|
import UIKit
|
||
|
|
|
||
|
|
/// icon: 12x12 textLabel: txt.label.s
|
||
|
|
class CLIconLabel: UIView {
|
||
|
|
// MARK: - Properties
|
||
|
|
|
||
|
|
/// Default: 12x12 icon image view
|
||
|
|
let iconImageView: UIImageView = {
|
||
|
|
let view = UIImageView()
|
||
|
|
view.translatesAutoresizingMaskIntoConstraints = false
|
||
|
|
return view
|
||
|
|
}()
|
||
|
|
|
||
|
|
/// Label with default text style
|
||
|
|
let contentLabel: UILabel = {
|
||
|
|
let label = UILabel()
|
||
|
|
label.translatesAutoresizingMaskIntoConstraints = false
|
||
|
|
label.textColor = .c.ctpn
|
||
|
|
label.font = .t.tls // 12
|
||
|
|
label.textAlignment = .left
|
||
|
|
return label
|
||
|
|
}()
|
||
|
|
|
||
|
|
private let stackView: UIStackView = {
|
||
|
|
let stackView = UIStackView()
|
||
|
|
stackView.translatesAutoresizingMaskIntoConstraints = false
|
||
|
|
stackView.spacing = 4
|
||
|
|
stackView.distribution = .equalSpacing
|
||
|
|
stackView.axis = .horizontal
|
||
|
|
stackView.alignment = .center
|
||
|
|
stackView.setContentHuggingPriority(UILayoutPriority(rawValue: 256), for: .horizontal)
|
||
|
|
return stackView
|
||
|
|
}()
|
||
|
|
|
||
|
|
/// Default: 12x12
|
||
|
|
var iconSize: CGSize?{
|
||
|
|
didSet{
|
||
|
|
iconImageView.snp.remakeConstraints { make in
|
||
|
|
make.size.equalTo(iconSize ?? CGSize(width: 12, height: 12))
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
/// Configurable left and right padding for the stack view
|
||
|
|
var innerLRPadding: CGFloat = 0 {
|
||
|
|
didSet {
|
||
|
|
// Update stack view constraints when padding changes
|
||
|
|
stackView.snp.remakeConstraints { make in
|
||
|
|
make.centerY.equalTo(self)
|
||
|
|
make.leading.equalTo(self).offset(self.innerLRPadding)
|
||
|
|
make.trailing.equalTo(self).offset(-self.innerLRPadding)
|
||
|
|
make.top.greaterThanOrEqualToSuperview()
|
||
|
|
make.bottom.lessThanOrEqualToSuperview()
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
var spacing:CGFloat = 4{
|
||
|
|
didSet{
|
||
|
|
stackView.spacing = spacing
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
// MARK: - Initialization
|
||
|
|
|
||
|
|
override init(frame: CGRect) {
|
||
|
|
super.init(frame: frame)
|
||
|
|
setupViews()
|
||
|
|
}
|
||
|
|
|
||
|
|
required init?(coder: NSCoder) {
|
||
|
|
super.init(coder: coder)
|
||
|
|
setupViews()
|
||
|
|
}
|
||
|
|
|
||
|
|
// MARK: - Setup
|
||
|
|
|
||
|
|
private func setupViews() {
|
||
|
|
stackView.setContentHuggingPriority(UILayoutPriority(258), for: .horizontal)
|
||
|
|
iconImageView.setContentHuggingPriority(UILayoutPriority(rawValue: 254), for: .horizontal)
|
||
|
|
iconImageView.setContentCompressionResistancePriority(UILayoutPriority(rawValue: 754), for: .horizontal)
|
||
|
|
contentLabel.setContentHuggingPriority(UILayoutPriority(rawValue: 254), for: .horizontal)
|
||
|
|
contentLabel.setContentCompressionResistancePriority(UILayoutPriority(rawValue: 754), for: .horizontal)
|
||
|
|
|
||
|
|
addSubview(stackView)
|
||
|
|
|
||
|
|
// Configure stack view constraints
|
||
|
|
stackView.snp.makeConstraints { make in
|
||
|
|
make.leading.equalTo(self).offset(innerLRPadding)
|
||
|
|
make.trailing.equalTo(self).offset(-innerLRPadding)
|
||
|
|
make.centerY.equalTo(self)
|
||
|
|
make.top.greaterThanOrEqualToSuperview()
|
||
|
|
make.bottom.lessThanOrEqualToSuperview()
|
||
|
|
}
|
||
|
|
|
||
|
|
// Add subviews to stack view
|
||
|
|
stackView.addArrangedSubview(iconImageView)
|
||
|
|
stackView.addArrangedSubview(contentLabel)
|
||
|
|
|
||
|
|
// Set icon image view size (12x12 as per original)
|
||
|
|
iconImageView.snp.makeConstraints { make in
|
||
|
|
make.size.equalTo(CGSize(width: 12, height: 12))
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
// MARK: - Helper
|
||
|
|
|
||
|
|
private func commonCoinAndLabel(){
|
||
|
|
|
||
|
|
}
|
||
|
|
}
|