import SnapKit import UIKit /// 机巢新版界面的局部样式,不改变其他业务页面的主题。 enum DroneDockStyle { static let primary = UIColor(hex: 0x3164FF) static let background = UIColor(hex: 0xF5F6FA) static let text = UIColor(hex: 0x172033) static let secondary = UIColor(hex: 0x8992A3) static let border = UIColor(hex: 0xE8ECF3) static let success = UIColor(hex: 0x169B6B) static let warning = UIColor(hex: 0xD68A28) /// 将模型的语义色映射为界面颜色。 static func color(for tone: DroneDockStatusTone) -> UIColor { switch tone { case .blue: primary case .green: success case .orange: warning case .red: UIColor(hex: 0xD46B62) case .gray: secondary } } /// 白色圆角卡片的统一装饰。 static func applyCard(to view: UIView) { view.backgroundColor = .white view.layer.cornerRadius = 16 view.layer.cornerCurve = .continuous } /// 创建常用文字,显式传入字重及颜色。 static func label(_ text: String? = nil, size: CGFloat = 14, weight: UIFont.Weight = .regular, color: UIColor = DroneDockStyle.text) -> UILabel { let label = UILabel() label.text = text label.font = .systemFont(ofSize: size, weight: weight) label.textColor = color return label } } /// 有内边距的状态标签,在两种订单卡和设备卡中保持一致。 final class DroneDockStatusLabel: UILabel { private let padding = UIEdgeInsets(top: 4, left: 7, bottom: 4, right: 7) /// 创建自适应文字宽度的浅色标签。 override init(frame: CGRect) { super.init(frame: frame) font = .systemFont(ofSize: 11, weight: .medium) layer.cornerRadius = 6 clipsToBounds = true setContentHuggingPriority(.required, for: .horizontal) setContentCompressionResistancePriority(.required, for: .horizontal) } @available(*, unavailable) required init?(coder: NSCoder) { fatalError("init(coder:) has not been implemented") } /// 应用状态文字及对应语义颜色。 func apply(title: String, tone: DroneDockStatusTone) { text = title textColor = DroneDockStyle.color(for: tone) backgroundColor = textColor.withAlphaComponent(0.09) } override var intrinsicContentSize: CGSize { let size = super.intrinsicContentSize return CGSize(width: size.width + padding.left + padding.right, height: size.height + padding.top + padding.bottom) } override func drawText(in rect: CGRect) { super.drawText(in: rect.inset(by: padding)) } }