mastodon-ios/Mastodon/Scene/Share/View/Content/DoubleTitleLabelNavigationB...

90 lines
2.3 KiB
Swift
Raw Normal View History

2021-04-02 04:21:51 +02:00
//
// DoubleTitleLabelNavigationBarTitleView.swift
2021-04-02 04:21:51 +02:00
// Mastodon
//
// Created by BradGao on 2021/4/1.
//
import UIKit
final class DoubleTitleLabelNavigationBarTitleView: UIView {
2021-04-02 04:21:51 +02:00
let containerView = UIStackView()
2021-04-02 10:38:33 +02:00
let titleLabel: UILabel = {
let label = UILabel()
label.font = .systemFont(ofSize: 17, weight: .semibold)
label.textColor = Asset.Colors.Label.primary.color
label.textAlignment = .center
return label
}()
2021-04-02 04:21:51 +02:00
2021-04-02 10:38:33 +02:00
let subtitleLabel: UILabel = {
let label = UILabel()
label.font = .systemFont(ofSize: 12)
label.textColor = Asset.Colors.Label.secondary.color
label.textAlignment = .center
label.isHidden = true
return label
}()
2021-04-02 04:21:51 +02:00
override init(frame: CGRect) {
super.init(frame: frame)
_init()
}
required init?(coder: NSCoder) {
super.init(coder: coder)
_init()
}
}
extension DoubleTitleLabelNavigationBarTitleView {
2021-04-02 04:21:51 +02:00
private func _init() {
2021-04-02 10:38:33 +02:00
containerView.axis = .vertical
containerView.alignment = .center
containerView.distribution = .fill
2021-04-02 04:21:51 +02:00
containerView.translatesAutoresizingMaskIntoConstraints = false
addSubview(containerView)
NSLayoutConstraint.activate([
containerView.topAnchor.constraint(equalTo: topAnchor),
containerView.leadingAnchor.constraint(equalTo: leadingAnchor),
containerView.trailingAnchor.constraint(equalTo: trailingAnchor),
containerView.bottomAnchor.constraint(equalTo: bottomAnchor),
])
2021-04-02 10:38:33 +02:00
containerView.addArrangedSubview(titleLabel)
containerView.addArrangedSubview(subtitleLabel)
}
func update(title: String, subtitle: String?) {
titleLabel.text = title
if let subtitle = subtitle {
subtitleLabel.text = subtitle
2021-04-02 10:38:33 +02:00
subtitleLabel.isHidden = false
} else {
subtitleLabel.text = nil
subtitleLabel.isHidden = true
}
2021-04-02 04:21:51 +02:00
}
}
#if canImport(SwiftUI) && DEBUG
import SwiftUI
struct DoubleTitleLabelNavigationBarTitleView_Previews: PreviewProvider {
static var previews: some View {
UIViewPreview(width: 375) {
DoubleTitleLabelNavigationBarTitleView()
}
.previewLayout(.fixed(width: 375, height: 100))
}
}
#endif