-
Notifications
You must be signed in to change notification settings - Fork 184
/
CollectionViewCell.swift
111 lines (86 loc) · 2.79 KB
/
CollectionViewCell.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
//
// RAMCell.swift
// RAMReel
//
// Created by Mikhail Stepkin on 4/10/15.
// Copyright (c) 2015 Ramotion. All rights reserved.
//
import UIKit
// MARK: - Collection view cells
/**
Type that implements this protocol allows configuration.
As type name hints this protocol primarily targeted to UITableView and UICollectionView cells
*/
public protocol ConfigurableCell {
associatedtype DataType: Equatable
/**
Implementing type should use data to fill own data fields
- parameter data: Data to present in the cell
*/
func configureCell(_ data: DataType)
/// Visual appearance theme
var theme: Theme { get set }
}
/**
RAMCell
--
Example configurable cell
*/
open class RAMCell: UICollectionViewCell, ConfigurableCell {
/**
Proxy call to superclass init.
- parameter coder: `NSCoder` instance proxied to superview.
*/
required public init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
setup()
}
/**
Proxy call to superclass init.
- parameter frame: Rect of cell, proxied to superview.
*/
public override init(frame: CGRect) {
super.init(frame: frame)
setup()
}
var textLabel: UILabel!
/// Visual appearance theme
open var theme: Theme = RAMTheme.sharedTheme {
didSet {
if theme.font != oldValue.font || theme.textColor != oldValue.textColor {
updateFont()
}
}
}
func updateFont() {
let theme = self.theme
textLabel.font = theme.font
textLabel.textColor = theme.textColor.withAlphaComponent(0.3)
}
fileprivate func setup() {
let labelFrame = self.contentView.bounds
textLabel = UILabel(frame: labelFrame)
textLabel.translatesAutoresizingMaskIntoConstraints = false
self.contentView.addSubview(textLabel)
let attributes: [NSLayoutConstraint.Attribute] = [.left, .right, .top, .bottom]
let constraints: [NSLayoutConstraint] = attributes.map {
let const: CGFloat = ($0 == .left || $0 == .right) ? -20 : 0
return NSLayoutConstraint(item: self,
attribute: $0,
relatedBy: .equal,
toItem: textLabel,
attribute: $0,
multiplier: 1,
constant: const)
}
addConstraints(constraints)
updateFont()
}
/**
Applies string data to the label text property
- parameter string: String to show in the cell
*/
open func configureCell(_ string: String) {
self.textLabel.text = string
}
}