-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathSelectableTokenView.swift
More file actions
134 lines (88 loc) · 3.67 KB
/
SelectableTokenView.swift
File metadata and controls
134 lines (88 loc) · 3.67 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
//
// SelectableTokenView.swift
// SelectableView
//
// Created by Amornchai Kanokpullwad on 10/11/2014.
// Simplified and rewritten in Swift by Bartłomiej Semańczyk and Piotr Pawluś
// Copyright (c) 2016 Bartłomiej Semańczyk. All rights reserved.
//
open class VerticalTokenView: UIControl {
var selectableView: MultiSelectableView?
private var tokenViews = [UIView]()
private var placeholderLabel = UILabel()
//MARK: - Class Methods
//MARK: - Initialization
//MARK: - Deinitialization
//MARK: - Actions
//MARK: - Open
//MARK: - Internal
func reloadData() {
if let options = selectableView?.sortedSelectedOptions {
for subview in subviews {
subview.removeFromSuperview()
}
tokenViews.removeAll()
for index in 0..<options.count {
if let tokenView = selectableView?.tokenView(for: options[index]) {
tokenView.autoresizingMask = UIView.AutoresizingMask()
addSubview(tokenView)
tokenViews.append(tokenView)
}
}
invalidateIntrinsicContentSize()
if options.isEmpty {
setupPlaceholderLabel()
addSubview(placeholderLabel)
}
}
}
func setupPlaceholderLabel() {
if let selectableView = selectableView {
placeholderLabel.frame = CGRect(x: CGFloat(selectableView.leftPaddingForPlaceholderText), y: 0, width: frame.size.width, height: selectableView.lineHeight)
placeholderLabel.text = selectableView.placeholder
placeholderLabel.textColor = selectableView.textColorForPlaceholderText
placeholderLabel.font = selectableView.fontForPlaceholderText
}
}
//MARK: - Private
private func enumerateItemRectsUsingBlock(_ block: (CGRect) -> Void) {
var x: CGFloat = 0
var y: CGFloat = 0
let lineHeight = selectableView?.lineHeight ?? 0
let margin = selectableView?.margin ?? 0
for token in tokenViews {
let width = max(bounds.width, token.frame.width)
let tokenWidth = min(bounds.width, token.frame.width)
if x > (width - tokenWidth) {
y += lineHeight + margin
x = 0
}
block(CGRect(x: x, y: y, width: tokenWidth, height: token.frame.size.height))
x += tokenWidth + margin
}
}
//MARK: - Overridden
override open func layoutSubviews() {
super.layoutSubviews()
invalidateIntrinsicContentSize()
var counter = 0
enumerateItemRectsUsingBlock { frame in
let token = self.tokenViews[counter]
token.frame = frame
counter += 1
}
}
override open var intrinsicContentSize : CGSize {
let lineHeight = selectableView?.lineHeight ?? 0
if tokenViews.isEmpty {
selectableView?.tokenViewHeightConstraint?.constant = lineHeight
return CGSize.zero
}
var totalRect = CGRect.null
enumerateItemRectsUsingBlock { itemRect in
totalRect = itemRect.union(totalRect)
}
selectableView?.tokenViewHeightConstraint?.constant = max(totalRect.size.height, lineHeight)
return totalRect.size
}
}