-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathDropdownTitleView.swift
More file actions
180 lines (150 loc) · 5.95 KB
/
DropdownTitleView.swift
File metadata and controls
180 lines (150 loc) · 5.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
//
// NavigationTitleDropdownView.swift
// Freetime
//
// Created by Ryan Nystrom on 1/13/18.
// Copyright © 2018 Ryan Nystrom. All rights reserved.
//
import UIKit
open class DropdownTitleView: UIControl {
internal let titleLabel = UILabel()
internal let subtitleLabel = UILabel()
internal let chevron = UIImageView(image: UIImage(
named: "chevron-down-small",
in: Bundle(for: DropdownTitleView.self),
compatibleWith: nil
)?.withRenderingMode(.alwaysTemplate)
)
internal let horizontalStackView = UIStackView()
internal let verticalStackView = UIStackView()
public init() {
super.init(frame: .zero)
isAccessibilityElement = true
accessibilityTraits |= UIAccessibilityTraitButton
chevron.translatesAutoresizingMaskIntoConstraints = false
chevron.setContentCompressionResistancePriority(.required, for: .horizontal)
chevron.setContentCompressionResistancePriority(.required, for: .vertical)
chevron.setContentHuggingPriority(.required, for: .horizontal)
chevron.setContentHuggingPriority(.required, for: .vertical)
titleLabel.translatesAutoresizingMaskIntoConstraints = false
titleLabel.backgroundColor = .clear
titleLabel.textAlignment = .center
titleLabel.lineBreakMode = .byTruncatingHead
titleLabel.adjustsFontSizeToFitWidth = true
titleLabel.minimumScaleFactor = 0.75
titleLabel.textColor = UIColor.darkText
titleLabel.font = UIFont.boldSystemFont(ofSize: UIFont.systemFontSize)
titleLabel.isAccessibilityElement = false
subtitleLabel.translatesAutoresizingMaskIntoConstraints = false
subtitleLabel.backgroundColor = .clear
subtitleLabel.textAlignment = .center
subtitleLabel.lineBreakMode = .byTruncatingHead
subtitleLabel.adjustsFontSizeToFitWidth = true
subtitleLabel.minimumScaleFactor = 0.75
subtitleLabel.textColor = UIColor.darkGray
subtitleLabel.font = UIFont.systemFont(ofSize: UIFont.smallSystemFontSize)
subtitleLabel.isAccessibilityElement = false
verticalStackView.isUserInteractionEnabled = false
verticalStackView.translatesAutoresizingMaskIntoConstraints = false
verticalStackView.alignment = .center
verticalStackView.axis = .vertical
verticalStackView.distribution = .equalCentering
verticalStackView.addArrangedSubview(titleLabel)
verticalStackView.addArrangedSubview(subtitleLabel)
horizontalStackView.isUserInteractionEnabled = false
horizontalStackView.translatesAutoresizingMaskIntoConstraints = false
horizontalStackView.alignment = .center
horizontalStackView.distribution = .equalCentering
horizontalStackView.spacing = 4
horizontalStackView.addArrangedSubview(verticalStackView)
horizontalStackView.addArrangedSubview(chevron)
addSubview(horizontalStackView)
horizontalStackView.centerXAnchor.constraint(equalTo: centerXAnchor).isActive = true
horizontalStackView.centerYAnchor.constraint(equalTo: centerYAnchor).isActive = true
addConstraint(NSLayoutConstraint(item: horizontalStackView, attribute: .width, relatedBy: .lessThanOrEqual, toItem: self, attribute: .width, multiplier: 1, constant: 0))
addConstraint(NSLayoutConstraint(item: horizontalStackView, attribute: .height, relatedBy: .lessThanOrEqual, toItem: self, attribute: .height, multiplier: 1, constant: 0))
}
required public init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
@objc dynamic open var chevronTintColor: UIColor? {
get {
return chevron.tintColor
}
set {
chevron.tintColor = newValue
}
}
@objc dynamic open var titleFont: UIFont {
get {
return titleLabel.font
}
set {
titleLabel.font = newValue
invalidateIntrinsicContentSize()
}
}
@objc dynamic open var titleColor: UIColor {
get {
return titleLabel.textColor
}
set {
titleLabel.textColor = newValue
}
}
@objc dynamic open var subtitleFont: UIFont {
get {
return subtitleLabel.font
}
set {
subtitleLabel.font = newValue
invalidateIntrinsicContentSize()
}
}
@objc dynamic open var subtitleColor: UIColor {
get {
return subtitleLabel.textColor
}
set {
subtitleLabel.textColor = newValue
}
}
open override var isHighlighted: Bool {
didSet {
fadeControls(alpha: isHighlighted ? 0.5 : 1)
}
}
open override func sizeThatFits(_ size: CGSize) -> CGSize {
return horizontalStackView.systemLayoutSizeFitting(size)
}
open override func systemLayoutSizeFitting(_ targetSize: CGSize) -> CGSize {
return horizontalStackView.systemLayoutSizeFitting(targetSize)
}
override open var intrinsicContentSize: CGSize {
return UILayoutFittingCompressedSize
}
// MARK: Public API
open func configure(
title: String,
subtitle: String? = nil,
chevronEnabled: Bool = true,
accessibilityLabel: String? = nil,
accessibilityHint: String? = nil
) {
titleLabel.text = title
subtitleLabel.text = subtitle
self.accessibilityLabel = accessibilityLabel ?? title
self.accessibilityHint = accessibilityHint
chevron.isHidden = !chevronEnabled
subtitleLabel.isHidden = !(subtitle?.isEmpty == false)
invalidateIntrinsicContentSize()
}
// MARK: Private API
internal var chevronEnabled: Bool {
return chevron.superview != nil
}
internal func fadeControls(alpha: CGFloat) {
guard chevronEnabled else { return }
[titleLabel, subtitleLabel, chevron].forEach { $0.alpha = alpha }
}
}