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

99 lines
2.5 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
2021-07-23 13:10:27 +02:00
import Meta
import MetaTextKit
import MastodonAsset
2022-10-08 07:43:06 +02:00
import MastodonCore
import MastodonUI
import MastodonLocalization
2021-04-02 04:21:51 +02:00
final class DoubleTitleLabelNavigationBarTitleView: UIView {
2021-04-02 04:21:51 +02:00
let containerView = UIStackView()
2021-07-23 13:10:27 +02:00
let titleLabel = MetaLabel(style: .titleView)
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)
}
2021-07-23 13:10:27 +02:00
func update(title: String, subtitle: String?) {
titleLabel.configure(content: PlaintextMetaContent(string: title))
update(subtitle: subtitle)
}
2021-04-02 10:38:33 +02:00
2021-07-23 13:10:27 +02:00
func update(titleMetaContent: MetaContent, subtitle: String?) {
titleLabel.configure(content: titleMetaContent)
update(subtitle: subtitle)
}
func update(subtitle: String?) {
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