Files
suixinkan_uikit/suixinkan/UI/TravelAlbum/WiredTransferSettingChipButton.swift
2026-07-31 16:48:28 +08:00

95 lines
3.3 KiB
Swift
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//
// WiredTransferSettingChipButton.swift
// suixinkan
//
import SnapKit
import UIKit
/// 线 chip `UIButton` title
final class WiredTransferSettingChipButton: UIButton {
private let titleLabelView = UILabel()
private let chevronView = UIImageView()
private var titleTrailingToSuperviewConstraint: Constraint?
private var titleTrailingToChevronConstraint: Constraint?
///
var showsChevron: Bool {
didSet { updateChevronLayout() }
}
///
private(set) var titleText: String = ""
///
var displayedTitleColor: UIColor? {
titleLabelView.textColor
}
/// chip
/// - Parameter showsChevron:
init(showsChevron: Bool = false) {
self.showsChevron = showsChevron
super.init(frame: .zero)
setupUI()
updateChevronLayout()
}
@available(*, unavailable)
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
/// chip
func apply(title: String) {
titleText = title
titleLabelView.text = title
accessibilityLabel = title
}
private func setupUI() {
backgroundColor = AppColor.pageBackground
layer.cornerRadius = 8
layer.borderWidth = 1
layer.borderColor = AppColor.border.cgColor
titleLabelView.font = .systemFont(ofSize: 11)
// 使使
titleLabelView.textColor = AppColor.textPrimary
titleLabelView.lineBreakMode = .byTruncatingTail
titleLabelView.isUserInteractionEnabled = false
chevronView.image = UIImage(systemName: "chevron.down")?
.withConfiguration(UIImage.SymbolConfiguration(pointSize: 13, weight: .semibold))
chevronView.tintColor = AppColor.textSecondary
chevronView.contentMode = .scaleAspectFit
chevronView.isUserInteractionEnabled = false
addSubview(titleLabelView)
addSubview(chevronView)
titleLabelView.snp.makeConstraints { make in
make.leading.equalToSuperview().offset(10)
make.centerY.equalToSuperview()
titleTrailingToSuperviewConstraint = make.trailing.lessThanOrEqualToSuperview().offset(-10).constraint
titleTrailingToChevronConstraint = make.trailing.lessThanOrEqualTo(chevronView.snp.leading).offset(-4).constraint
}
chevronView.snp.makeConstraints { make in
make.trailing.equalToSuperview().offset(-10)
make.centerY.equalToSuperview()
make.size.equalTo(16)
}
}
private func updateChevronLayout() {
chevronView.isHidden = !showsChevron
if showsChevron {
titleTrailingToSuperviewConstraint?.deactivate()
titleTrailingToChevronConstraint?.activate()
} else {
titleTrailingToChevronConstraint?.deactivate()
titleTrailingToSuperviewConstraint?.activate()
}
}
}