-
Notifications
You must be signed in to change notification settings - Fork 1
/
UIFloatMenuActionCell.swift
231 lines (197 loc) · 8.21 KB
/
UIFloatMenuActionCell.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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
//
// UIFloatMenuActionCell.swift
// UIFloatMenu
//
import UIKit
class UIFloatMenuActionCell: UITableViewCell {
var itemColor: itemColor!
var itemLayout: cellLayout!
var itemHeight: heightStyle!
var initBackColor: UIColor!
// MARK: Views
private var contentStackView: UIStackView = UIStackView()
var backView: UIView = {
let uiview = UIView()
uiview.layer.cornerRadius = 8
uiview.isUserInteractionEnabled = false
return uiview
}()
var titleLabel: UILabel = {
let label = UILabel()
label.textColor = UIFloatMenuColors.revColor
label.numberOfLines = 1
label.isUserInteractionEnabled = false
label.font = UIFloatMenuHelper.roundedFont(fontSize: 16, weight: .semibold)
return label
}()
var subtitleLabel: UILabel = {
let label = UILabel()
label.textColor = UIFloatMenuColors.revColor
label.numberOfLines = 1
label.font = UIFloatMenuHelper.roundedFont(fontSize: 11, weight: .semibold)
label.alpha = 0.8
label.isUserInteractionEnabled = false
return label
}()
var iconImageView: UIImageView = {
let imageView = UIImageView()
imageView.contentMode = .scaleAspectFit
imageView.isUserInteractionEnabled = false
return imageView
}()
// MARK: init
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
selectionStyle = .none
backgroundColor = .clear
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
// MARK: setHighlighted
override func setHighlighted(_ highlighted: Bool, animated: Bool) {
super.setHighlighted(highlighted, animated: animated)
if highlighted {
switch itemColor {
case .clear:
let color = UIFloatMenuHelper.detectTheme(dark: UIColor.lightGray.withAlphaComponent(0.08),
light: UIColor.lightGray.withAlphaComponent(0.12),
any: UIColor.lightGray.withAlphaComponent(0.08))
backView.backgroundColor = color
break
case .tinted(_):
break
case .filled(_):
backView.alpha = 0.9
break
case .standard:
backView.alpha = 0.9
break
case .custom(_, let tintColor, let backColor):
if backColor == .clear {
backView.backgroundColor = tintColor.withAlphaComponent(0.1)
}
break
case .none:
break
}
} else {
backView.backgroundColor = initBackColor
backView.alpha = 1
}
}
// MARK: layoutSubviews
override func layoutSubviews() {
super.layoutSubviews()
contentView.addSubview(backView)
if iconImageView.image != nil {
backView.addSubview(iconImageView)
}
backView.frame.size = CGSize(width: frame.width-20, height: frame.height-5)
backView.center.x = frame.width/2
backView.center.y = frame.height/2
contentStackView.axis = .vertical
contentStackView.distribution = .fillProportionally
if subtitleLabel.text != "" {
contentStackView.addArrangedSubview(titleLabel)
contentStackView.addArrangedSubview(subtitleLabel)
} else {
contentStackView.addArrangedSubview(titleLabel)
}
addSubview(contentStackView)
contentStackView.translatesAutoresizingMaskIntoConstraints = false
var top_bottom: CGFloat {
switch itemHeight {
case .standard:
return 10
case .compact:
return 7
case .big:
return 16
case .none:
break
}
return 0
}
switch itemLayout {
case .Icon_Title:
if iconImageView.image != nil {
iconImageView.frame.origin.x = 10
}
NSLayoutConstraint.activate([
contentStackView.topAnchor.constraint(equalTo: backView.topAnchor, constant: top_bottom),
contentStackView.leadingAnchor.constraint(equalTo: backView.leadingAnchor, constant: iconImageView.image != nil ? 40 : 10),
contentStackView.bottomAnchor.constraint(equalTo: backView.bottomAnchor, constant: -top_bottom),
contentStackView.trailingAnchor.constraint(equalTo: backView.trailingAnchor, constant: -10)
])
break
case .Title_Icon:
if iconImageView.image != nil {
iconImageView.frame.origin.x = backView.frame.width-35
}
NSLayoutConstraint.activate([
contentStackView.topAnchor.constraint(equalTo: backView.topAnchor, constant: top_bottom),
contentStackView.leadingAnchor.constraint(equalTo: backView.leadingAnchor, constant: 10),
contentStackView.bottomAnchor.constraint(equalTo: backView.bottomAnchor, constant: -top_bottom),
contentStackView.trailingAnchor.constraint(equalTo: backView.trailingAnchor, constant: iconImageView.image != nil ? -40 : -10)
])
break
case .none:
break
}
contentStackView.isUserInteractionEnabled = false
iconImageView.frame.size = CGSize(width: 23, height: 23)
iconImageView.center.y = backView.frame.height/2
switch itemColor {
case .clear:
if iconImageView.image != nil {
let templateImage = iconImageView.image?.withRenderingMode(.alwaysTemplate)
iconImageView.image = templateImage
iconImageView.tintColor = UIFloatMenuColors.revColor
}
break
case .tinted(let color):
if iconImageView.image != nil {
let templateImage = iconImageView.image?.withRenderingMode(.alwaysTemplate)
iconImageView.image = templateImage
iconImageView.tintColor = color
}
titleLabel.textColor = color
subtitleLabel.textColor = color.withAlphaComponent(0.9)
backView.backgroundColor = color.withAlphaComponent(0.1)
break
case .filled(let color):
if iconImageView.image != nil {
let templateImage = iconImageView.image?.withRenderingMode(.alwaysTemplate)
iconImageView.image = templateImage
iconImageView.tintColor = .white
}
titleLabel.textColor = .white
subtitleLabel.textColor = UIColor.white.withAlphaComponent(0.9)
backView.backgroundColor = color
break
case .standard:
if iconImageView.image != nil {
let templateImage = iconImageView.image?.withRenderingMode(.alwaysTemplate)
iconImageView.image = templateImage
iconImageView.tintColor = UIFloatMenuColors.revColor
}
titleLabel.textColor = UIFloatMenuColors.revColor
subtitleLabel.textColor = UIFloatMenuColors.revColor
backView.backgroundColor = UIColor.lightGray.withAlphaComponent(0.08)
break
case .custom(let iconColor, let textColor, let backColor):
let image = iconColor != .clear ? iconImageView.image?.withRenderingMode(.alwaysTemplate) : iconImageView.image
iconImageView.image = image
iconImageView.tintColor = iconColor
titleLabel.textColor = textColor
subtitleLabel.textColor = textColor.withAlphaComponent(0.9)
if backColor != .clear {
backView.backgroundColor = backColor
}
break
case .none:
break
}
}
}