// // 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 = "" /// 创建设置 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.textTertiary titleLabelView.lineBreakMode = .byTruncatingTail titleLabelView.isUserInteractionEnabled = false chevronView.image = UIImage(systemName: "chevron.down")? .withConfiguration(UIImage.SymbolConfiguration(pointSize: 13, weight: .semibold)) chevronView.tintColor = AppColor.textTertiary 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() } } }