forked from GitHawkApp/MessageViewController
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMessageView.swift
More file actions
246 lines (198 loc) · 7.26 KB
/
MessageView.swift
File metadata and controls
246 lines (198 loc) · 7.26 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
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
//
// MessageView.swift
// MessageView
//
// Created by Ryan Nystrom on 12/20/17.
// Copyright © 2017 Ryan Nystrom. All rights reserved.
//
import UIKit
public final class MessageView: UIView, MessageTextViewListener {
public let textView = MessageTextView()
internal weak var delegate: MessageViewDelegate?
internal let button = UIButton()
internal let UITextViewContentSizeKeyPath = #keyPath(UITextView.contentSize)
internal let topBorderLayer = CALayer()
internal var contentView: UIView?
internal var buttonAction: Selector?
internal override init(frame: CGRect) {
super.init(frame: frame)
backgroundColor = .white
addSubview(textView)
addSubview(button)
layer.addSublayer(topBorderLayer)
// setup text view
textView.contentInset = .zero
textView.textContainerInset = .zero
textView.backgroundColor = .clear
textView.addObserver(self, forKeyPath: UITextViewContentSizeKeyPath, options: [.new], context: nil)
textView.font = .systemFont(ofSize: UIFont.systemFontSize)
textView.add(listener: self)
// setup TextKit props to defaults
textView.textContainer.exclusionPaths = []
textView.textContainer.maximumNumberOfLines = 0
textView.textContainer.lineFragmentPadding = 0
textView.layoutManager.allowsNonContiguousLayout = false
textView.layoutManager.hyphenationFactor = 0
textView.layoutManager.showsInvisibleCharacters = false
textView.layoutManager.showsControlCharacters = false
textView.layoutManager.usesFontLeading = true
// setup send button
button.titleEdgeInsets = .zero
button.contentEdgeInsets = .zero
button.imageEdgeInsets = .zero
updateEmptyTextStates()
}
public required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
deinit {
textView.removeObserver(self, forKeyPath: UITextViewContentSizeKeyPath)
}
// MARK: Public API
public var font: UIFont? {
get { return textView.font }
set {
textView.font = newValue
delegate?.wantsLayout(messageView: self)
}
}
public var text: String {
get { return textView.text ?? "" }
set {
textView.text = newValue
delegate?.wantsLayout(messageView: self)
updateEmptyTextStates()
}
}
public var inset: UIEdgeInsets = .zero {
didSet {
setNeedsLayout()
delegate?.wantsLayout(messageView: self)
}
}
public var buttonLeftInset: CGFloat = 0 {
didSet { setNeedsLayout() }
}
public func set(buttonIcon: UIImage?, for state: UIControlState) {
button.setImage(buttonIcon, for: state)
buttonLayoutDidChange()
}
public func set(buttonTitle: String, for state: UIControlState) {
button.setTitle(buttonTitle, for: state)
buttonLayoutDidChange()
}
public var buttonTint: UIColor {
get { return button.tintColor }
set {
button.tintColor = newValue
button.setTitleColor(newValue, for: .normal)
button.imageView?.tintColor = newValue
}
}
public var maxLineCount: Int = 4 {
didSet {
delegate?.wantsLayout(messageView: self)
}
}
public func add(contentView: UIView) {
self.contentView?.removeFromSuperview()
assert(contentView.bounds.height > 0, "Must have a non-zero content height")
self.contentView = contentView
addSubview(contentView)
setNeedsLayout()
delegate?.wantsLayout(messageView: self)
}
public var keyboardType: UIKeyboardType {
get { return textView.keyboardType }
set { textView.keyboardType = newValue }
}
public func addButton(target: Any, action: Selector) {
button.addTarget(target, action: action, for: .touchUpInside)
buttonAction = action
}
public override var keyCommands: [UIKeyCommand]? {
guard let action = buttonAction else { return nil }
return [UIKeyCommand(input: "\r", modifierFlags: .command, action: action)]
}
// MARK: Overrides
public override func layoutSubviews() {
super.layoutSubviews()
topBorderLayer.frame = CGRect(
x: bounds.minX,
y: bounds.minY,
width: bounds.width,
height: 1 / UIScreen.main.scale
)
let safeBounds = CGRect(
x: bounds.minX + util_safeAreaInsets.left,
y: bounds.minY,
width: bounds.width - util_safeAreaInsets.left - util_safeAreaInsets.right,
height: bounds.height
)
let insetBounds = UIEdgeInsetsInsetRect(safeBounds, inset)
let buttonSize = button.bounds.size
let textViewFrame = CGRect(
x: insetBounds.minX,
y: insetBounds.minY,
width: insetBounds.width - buttonSize.width - buttonLeftInset,
height: textViewHeight
)
textView.frame = textViewFrame
// adjust by bottom offset so content is flush w/ text view
button.frame = CGRect(
x: textViewFrame.maxX + buttonLeftInset,
y: textViewFrame.maxY - buttonSize.height + button.bottomHeightOffset,
width: buttonSize.width,
height: buttonSize.height
)
let contentY = textViewFrame.maxY + inset.bottom
contentView?.frame = CGRect(
x: safeBounds.minX,
y: contentY,
width: safeBounds.width,
height: bounds.height - contentY - util_safeAreaInsets.bottom
)
}
public override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
if keyPath == UITextViewContentSizeKeyPath {
textViewContentSizeDidChange()
}
}
public override func resignFirstResponder() -> Bool {
return textView.resignFirstResponder()
}
// MARK: Private API
internal var height: CGFloat {
return inset.top
+ inset.bottom
+ textViewHeight
+ (contentView?.bounds.height ?? 0)
}
internal var textViewHeight: CGFloat {
return min(maxHeight, max(textView.font?.lineHeight ?? 0, textView.contentSize.height))
}
internal var maxHeight: CGFloat {
return (font?.lineHeight ?? 0) * CGFloat(maxLineCount)
}
internal func updateEmptyTextStates() {
let isEmpty = text.isEmpty
button.isEnabled = !isEmpty
button.alpha = isEmpty ? 0.25 : 1
}
internal func buttonLayoutDidChange() {
button.sizeToFit()
setNeedsLayout()
}
internal func textViewContentSizeDidChange() {
delegate?.sizeDidChange(messageView: self)
textView.alwaysBounceVertical = textView.contentSize.height > maxHeight
}
// MARK: MessageTextViewListener
public func didChange(textView: MessageTextView) {
updateEmptyTextStates()
}
public func didChangeSelection(textView: MessageTextView) {
delegate?.selectionDidChange(messageView: self)
}
public func willChangeRange(textView: MessageTextView, to range: NSRange) {}
}