-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathTabItemViewCell.swift
187 lines (161 loc) · 5.8 KB
/
TabItemViewCell.swift
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
181
182
183
184
185
186
187
//
// TabItemViewCell.swift
//
// Made with ❤️ by Novum
//
// Copyright © Telefonica. All rights reserved.
//
import UIKit
public class TabItemViewCell: UICollectionViewCell {
private enum Constants {
static let cellHeight: CGFloat = 56
static let innerPadding: CGFloat = 16
static let bottomPadding: CGFloat = innerPadding - heightDivider
static let iconAndTextSpace: CGFloat = 8
static let iconSize: CGFloat = 24
static let heightDivider: CGFloat = 2
static let minimumItemWidthForLargeScreen: CGFloat = 208.0
}
private lazy var verticalStack: UIStackView = {
let verticalStack = UIStackView(arrangedSubviews: [horizontalStack, selectedLine])
verticalStack.axis = .vertical
verticalStack.distribution = .fill
verticalStack.alignment = .center
return verticalStack
}()
private lazy var horizontalStack: UIStackView = {
let horizontalStack = UIStackView(arrangedSubviews: [imageView, title])
horizontalStack.isLayoutMarginsRelativeArrangement = true
horizontalStack.directionalLayoutMargins = NSDirectionalEdgeInsets(
top: Constants.innerPadding,
leading: Constants.innerPadding,
bottom: Constants.bottomPadding,
trailing: Constants.innerPadding
)
horizontalStack.axis = .horizontal
horizontalStack.distribution = .fill
horizontalStack.alignment = .fill
horizontalStack.spacing = Constants.iconAndTextSpace
return horizontalStack
}()
private lazy var imageView: IntrinsictImageView = {
let imageView = IntrinsictImageView()
imageView.intrinsicWidth = Constants.iconSize
imageView.intrinsicHeight = Constants.iconSize
return imageView
}()
private var overridenAccessibilityLabel: String?
private lazy var title: UILabel = {
let label = UILabel()
label.font = .textPresetTabsLabel()
label.backgroundColor = .clear
label.lineBreakMode = .byTruncatingTail
return label
}()
private lazy var selectedLine: UIView = {
let selectedLine = UIView()
selectedLine.backgroundColor = .controlActivated
return selectedLine
}()
private lazy var minimumWidthConstraintForLargeScreen = {
contentView.widthAnchor.constraint(greaterThanOrEqualToConstant: Constants.minimumItemWidthForLargeScreen)
}()
override init(frame: CGRect) {
super.init(frame: frame)
commomInit()
}
public required init?(coder: NSCoder) {
super.init(coder: coder)
commomInit()
}
}
// MARK: Public API
extension TabItemViewCell {
var text: String? {
get {
title.text
}
set {
title.text = newValue
}
}
var icon: UIImage? {
get {
imageView.image
}
set {
imageView.image = newValue
if newValue == nil {
imageView.removeFromSuperview()
} else {
horizontalStack.insertArrangedSubview(imageView, at: 0)
}
}
}
func activeMinimumWidthConstraint(_ isActive: Bool) {
minimumWidthConstraintForLargeScreen.isActive = isActive
}
func showSelected() {
selectedLine.backgroundColor = .controlActivated
title.textColor = .textPrimary
imageView.tintColor = .neutralHigh
}
func showDeselected() {
selectedLine.backgroundColor = .clear
title.textColor = .textSecondary
imageView.tintColor = .neutralMedium
}
}
// MARK: - Private
private extension TabItemViewCell {
func commomInit() {
contentView.backgroundColor = .clear
backgroundColor = .clear
contentView.addSubview(verticalStack, constraints: [
verticalStack.topAnchor.constraint(equalTo: contentView.topAnchor),
verticalStack.trailingAnchor.constraint(equalTo: contentView.trailingAnchor),
verticalStack.bottomAnchor.constraint(equalTo: contentView.bottomAnchor),
verticalStack.leadingAnchor.constraint(equalTo: contentView.leadingAnchor),
selectedLine.heightAnchor.constraint(equalToConstant: Constants.heightDivider),
selectedLine.widthAnchor.constraint(equalTo: verticalStack.widthAnchor),
contentView.heightAnchor.constraint(equalToConstant: Constants.cellHeight),
imageView.heightAnchor.constraint(equalToConstant: Constants.iconSize),
minimumWidthConstraintForLargeScreen
])
setUpAccessibility()
}
func setUpAccessibility() {
// This component don't grow with Dynamic Type, but presents a HUD instead, since it is intended to be always
// presented below the navigation bar.
showsLargeContentViewer = true
addInteraction(UILargeContentViewerInteraction())
// For Voice Over, consider the whole cell as the accessibility element, since the cell is much bigger then the
// title label, and it is more difficult to find it. The accesibility label is overridden to just dynamically
// delegate on the title label
isAccessibilityElement = true
title.isAccessibilityElement = false
}
}
// MARK: Accessibility
public extension TabItemViewCell {
override var largeContentTitle: String? {
get { title.largeContentTitle }
set {}
}
override var largeContentImage: UIImage? {
get { imageView.image }
set {}
}
override var accessibilityLabel: String? {
get {
if overridenAccessibilityLabel != nil {
return overridenAccessibilityLabel
} else {
return title.text
}
}
set {
overridenAccessibilityLabel = newValue
}
}
}