-
-
Notifications
You must be signed in to change notification settings - Fork 452
/
SPBaseContentTableViewCell.swift
362 lines (307 loc) · 15.5 KB
/
SPBaseContentTableViewCell.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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
// The MIT License (MIT)
// Copyright © 2017 Ivan Vorobei (hello@ivanvorobei.by)
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
import UIKit
class SPBaseContentTableViewCell: SPTableViewCell {
let titleLabel = UILabel()
let subtitleLabel = UILabel()
let descriptionLabel = UILabel()
let iconImageView = SPDownloadingImageView()
let button = SPAppStoreActionButton()
var withImage: Bool = true {
didSet {
if withImage {
self.iconImageView.isHidden = false
self.iconImageWidthConstraint.constant = self.imageSide
self.iconImageHeightConstraint.constant = self.imageSide
self.titleLabelLeadingConstraint.isActive = true
self.separatorInsetStyle = .beforeImage
} else {
self.iconImageView.isHidden = true
self.iconImageWidthConstraint.constant = 0
self.iconImageHeightConstraint.constant = 0
self.titleLabelLeadingConstraint.isActive = false
self.separatorInsetStyle = .auto
}
self.updateConstraints()
}
}
var roundImage: Bool = false {
didSet {
self.layoutSubviews()
}
}
var withSubtitle: Bool = true {
didSet {
self.descriptionLabelTopConstraint.isActive = false
self.descriptionLabel.removeConstraint(self.descriptionLabelTopConstraint)
if withSubtitle {
self.subtitleLabel.isHidden = false
self.descriptionLabelTopConstraint = self.descriptionLabel.topAnchor.constraint(equalTo: self.subtitleLabel.bottomAnchor, constant: self.spaceAfterSubtitle)
} else {
self.subtitleLabel.isHidden = true
self.descriptionLabelTopConstraint = self.descriptionLabel.topAnchor.constraint(equalTo: self.titleLabel.bottomAnchor, constant: self.spaceAfterSubtitle)
}
self.descriptionLabelTopConstraint.isActive = true
self.updateConstraints()
}
}
var withButton: Bool = false {
didSet {
if withButton {
self.button.isHidden = false
self.descriptionLabelBottomConstraint.isActive = false
self.buttonTopConstraint.isActive = true
self.buttonBottomConstraint.isActive = true
} else {
self.button.isHidden = true
self.buttonTopConstraint.isActive = false
self.buttonBottomConstraint.isActive = false
self.descriptionLabelBottomConstraint.isActive = true
}
self.updateConstraints()
}
}
var centerXButton: Bool = false {
didSet {
if self.centerXButton {
self.buttonLeadingConstraint.isActive = false
self.buttonCenterXConstraint.isActive = true
} else {
self.buttonCenterXConstraint.isActive = false
self.buttonLeadingConstraint.isActive = true
}
self.updateConstraints()
}
}
var imageSide: CGFloat = 63 {
didSet {
self.iconImageWidthConstraint.constant = self.imageSide
self.iconImageHeightConstraint.constant = self.imageSide
self.updateConstraints()
}
}
var topSpace: CGFloat = 11 {
didSet {
self.iconImageTopConstraint.constant = self.topSpace
self.titleLabelTopConstraint.constant = self.topSpace - 2
self.updateConstraints()
}
}
var spaceAfterTitle: CGFloat = 2 {
didSet {
if self.withSubtitle {
self.subtitleLabelTopConstraint.constant = self.spaceAfterTitle
} else {
self.descriptionLabelTopConstraint.constant = self.spaceAfterTitle
}
self.updateConstraints()
}
}
var spaceAfterSubtitle: CGFloat = 2 {
didSet {
if self.withSubtitle {
self.descriptionLabelTopConstraint.constant = self.spaceAfterSubtitle
}
self.updateConstraints()
}
}
var spaceAfterDescribtion: CGFloat = 19 {
didSet {
self.buttonTopConstraint.constant = self.spaceAfterDescribtion
self.updateConstraints()
}
}
var bottomSpace: CGFloat = 7 {
didSet {
if self.withButton {
self.buttonBottomConstraint.constant = -self.bottomSpace
} else {
self.descriptionLabelBottomConstraint.constant = -self.bottomSpace
}
self.updateConstraints()
}
}
var spaceBetweenImageAndTitles: CGFloat = 14 {
didSet {
self.titleLabelLeadingConstraint.constant = self.spaceBetweenImageAndTitles
self.updateConstraints()
}
}
override var contentViews: [UIView] {
return [self.iconImageView, self.titleLabel, self.subtitleLabel, self.descriptionLabel]
}
//constraints
private var iconImageTopConstraint: NSLayoutConstraint!
private var iconImageBottomConstraint: NSLayoutConstraint!
private var iconImageWidthConstraint: NSLayoutConstraint!
private var iconImageHeightConstraint: NSLayoutConstraint!
private var titleLabelLeadingConstraint: NSLayoutConstraint!
private var titleLabelTopConstraint: NSLayoutConstraint!
private var subtitleLabelTopConstraint: NSLayoutConstraint!
private var descriptionLabelTopConstraint: NSLayoutConstraint!
private var descriptionLabelBottomConstraint: NSLayoutConstraint!
private var buttonTopConstraint: NSLayoutConstraint!
private var buttonLeadingConstraint: NSLayoutConstraint!
private var buttonCenterXConstraint: NSLayoutConstraint!
private var buttonBottomConstraint: NSLayoutConstraint!
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
self.commonInit()
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
private func commonInit() {
let marginGuide = contentView.layoutMarginsGuide
//iconImageView
self.iconImageView.layer.cornerRadius = 10
self.contentView.addSubview(self.iconImageView)
self.iconImageView.translatesAutoresizingMaskIntoConstraints = false
self.iconImageView.leadingAnchor.constraint(equalTo: marginGuide.leadingAnchor).isActive = true
self.iconImageTopConstraint = self.iconImageView.topAnchor.constraint(equalTo: marginGuide.topAnchor, constant: self.topSpace)
self.iconImageTopConstraint.isActive = true
self.iconImageWidthConstraint = self.iconImageView.widthAnchor.constraint(equalToConstant: self.imageSide)
self.iconImageWidthConstraint.isActive = true
self.iconImageHeightConstraint = self.iconImageView.heightAnchor.constraint(equalToConstant: self.imageSide)
self.iconImageHeightConstraint.isActive = true
self.iconImageBottomConstraint = self.iconImageView.bottomAnchor.constraint(lessThanOrEqualTo: marginGuide.bottomAnchor, constant: -(self.bottomSpace))
self.iconImageBottomConstraint.priority = UILayoutPriority.init(900)
self.iconImageBottomConstraint.isActive = true
//titleLabel
self.titleLabel.numberOfLines = 0
self.titleLabel.font = UIFont.system(type: UIFont.BoldType.Medium, size: 17)
self.titleLabel.textAlignment = .left
self.titleLabel.textColor = UIColor.black
self.contentView.addSubview(self.titleLabel)
self.titleLabel.translatesAutoresizingMaskIntoConstraints = false
self.titleLabelLeadingConstraint = self.titleLabel.leadingAnchor.constraint(equalTo: self.iconImageView.trailingAnchor, constant: self.spaceBetweenImageAndTitles)
self.titleLabelLeadingConstraint.isActive = true
let titleLabelAdditionalLeadingConstraint = self.titleLabel.leadingAnchor.constraint(equalTo: marginGuide.leadingAnchor)
titleLabelAdditionalLeadingConstraint.priority = UILayoutPriority.init(900)
titleLabelAdditionalLeadingConstraint.isActive = true
self.titleLabelTopConstraint = self.titleLabel.topAnchor.constraint(equalTo: marginGuide.topAnchor, constant: self.topSpace - 2)
self.titleLabelTopConstraint.isActive = true
self.titleLabel.trailingAnchor.constraint(equalTo: marginGuide.trailingAnchor).isActive = true
//subtitleLabel
self.subtitleLabel.numberOfLines = 0
self.subtitleLabel.font = UIFont.system(type: UIFont.BoldType.Regular, size: 15)
self.subtitleLabel.textAlignment = .left
self.subtitleLabel.textColor = UIColor.black
self.contentView.addSubview(self.subtitleLabel)
self.subtitleLabel.translatesAutoresizingMaskIntoConstraints = false
self.subtitleLabel.leadingAnchor.constraint(equalTo: self.titleLabel.leadingAnchor).isActive = true
self.subtitleLabel.trailingAnchor.constraint(equalTo: marginGuide.trailingAnchor).isActive = true
self.subtitleLabelTopConstraint = self.subtitleLabel.topAnchor.constraint(equalTo: self.titleLabel.bottomAnchor, constant: self.spaceAfterTitle)
self.subtitleLabelTopConstraint.isActive = true
//descriptionLabel
self.descriptionLabel.numberOfLines = 0
self.descriptionLabel.font = UIFont.system(type: UIFont.BoldType.Regular, size: 15)
self.descriptionLabel.textAlignment = .left
self.descriptionLabel.textColor = SPNativeStyleKit.Colors.gray
self.contentView.addSubview(self.descriptionLabel)
self.descriptionLabel.translatesAutoresizingMaskIntoConstraints = false
self.descriptionLabel.leadingAnchor.constraint(equalTo: self.titleLabel.leadingAnchor).isActive = true
self.descriptionLabel.trailingAnchor.constraint(equalTo: marginGuide.trailingAnchor).isActive = true
self.descriptionLabelTopConstraint = self.descriptionLabel.topAnchor.constraint(equalTo: self.subtitleLabel.bottomAnchor, constant: self.spaceAfterSubtitle)
self.descriptionLabelTopConstraint.isActive = true
self.descriptionLabelBottomConstraint = self.descriptionLabel.bottomAnchor.constraint(equalTo: marginGuide.bottomAnchor, constant: -self.bottomSpace)
self.descriptionLabelBottomConstraint.isActive = true
//button
self.contentView.addSubview(self.button)
self.button.titleLabel?.numberOfLines = 0
self.button.contentHorizontalAlignment = UIControl.ContentHorizontalAlignment.left
self.button.translatesAutoresizingMaskIntoConstraints = false
self.buttonLeadingConstraint = self.button.leadingAnchor.constraint(equalTo: self.titleLabel.leadingAnchor)
self.buttonLeadingConstraint.isActive = true
self.buttonCenterXConstraint = self.button.centerXAnchor.constraint(equalTo: self.titleLabel.centerXAnchor)
self.buttonCenterXConstraint.isActive = false
self.buttonTopConstraint = self.button.topAnchor.constraint(equalTo: self.descriptionLabel.bottomAnchor, constant: self.spaceAfterDescribtion)
self.buttonTopConstraint.isActive = false
self.buttonBottomConstraint = self.button.bottomAnchor.constraint(equalTo: marginGuide.bottomAnchor, constant: -self.bottomSpace)
self.buttonBottomConstraint.isActive = false
self.setDefaultParametrs()
}
private func setDefaultParametrs() {
self.withImage = true
self.withSubtitle = true
self.withButton = false
self.accessoryType = .none
self.separatorInsetStyle = .beforeImage
self.iconImageView.setLoadingMode()
self.iconImageView.setNativeStyle()
self.titleLabel.text = "Title"
self.titleLabel.font = UIFont.system(type: UIFont.BoldType.Medium, size: 17)
self.titleLabel.textAlignment = .left
self.titleLabel.textColor = UIColor.black
self.subtitleLabel.text = "Subtitle"
self.subtitleLabel.font = UIFont.system(type: UIFont.BoldType.Regular, size: 15)
self.subtitleLabel.textAlignment = .left
self.subtitleLabel.textColor = UIColor.black
self.descriptionLabel.text = "Description"
self.descriptionLabel.font = UIFont.system(type: UIFont.BoldType.Regular, size: 15)
self.descriptionLabel.textAlignment = .left
self.descriptionLabel.textColor = SPNativeStyleKit.Colors.gray
self.button.setTitle("Button")
self.imageSide = 63
self.roundImage = false
self.topSpace = 11
self.spaceAfterTitle = 2
self.spaceAfterSubtitle = 2
self.spaceAfterDescribtion = 15
self.bottomSpace = 7
self.spaceBetweenImageAndTitles = 15
/*self.subtitleLabel.backgroundColor = UIColor.red
self.titleLabel.backgroundColor = UIColor.blue
self.descriptionLabel.backgroundColor = UIColor.brown*/
}
override func setHighlighted(_ highlighted: Bool, animated: Bool) {
let myViewBackgroundColor = self.iconImageView.gradeView.backgroundColor
super.setHighlighted(highlighted, animated: animated)
self.iconImageView.gradeView.backgroundColor = myViewBackgroundColor
}
override func setSelected(_ selected: Bool, animated: Bool) {
let myViewBackgroundColor = self.iconImageView.gradeView.backgroundColor
super.setSelected(selected, animated: animated)
self.iconImageView.gradeView.backgroundColor = myViewBackgroundColor
}
override func prepareForReuse() {
super.prepareForReuse()
self.setDefaultParametrs()
}
override func layoutSubviews() {
super.layoutSubviews()
if self.roundImage {
self.iconImageView.layer.cornerRadius = self.imageSide / 2
} else {
self.iconImageView.layer.cornerRadius = self.imageSide * 0.158
}
switch self.separatorInsetStyle {
case .all:
self.separatorInset.left = 0
case .beforeImage:
self.separatorInset.left = self.layoutMargins.left + self.imageSide + self.spaceBetweenImageAndTitles - 2
case .none:
self.separatorInset.left = self.frame.width
case .auto:
self.separatorInset.left = self.layoutMargins.left
}
}
}