This repository was archived by the owner on Oct 8, 2022. It is now read-only.
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
341 lines (286 loc) · 10.5 KB
/
MessageView.swift
File metadata and controls
341 lines (286 loc) · 10.5 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
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
//
// 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 leftButton = UIButton()
internal let rightButton = UIButton()
internal let UITextViewContentSizeKeyPath = #keyPath(UITextView.contentSize)
internal let topBorderLayer = CALayer()
internal var contentView: UIView?
internal var leftButtonAction: Selector?
internal var rightButtonAction: Selector?
internal var leftButtonInset: CGFloat = 0
internal var rightButtonInset: CGFloat = 0
public enum ButtonPosition {
case left
case right
}
internal override init(frame: CGRect) {
super.init(frame: frame)
backgroundColor = .white
addSubview(leftButton)
addSubview(textView)
addSubview(rightButton)
layer.addSublayer(topBorderLayer)
//Set action button
leftButton.imageEdgeInsets = .zero
leftButton.titleEdgeInsets = .zero
leftButton.contentEdgeInsets = .zero
leftButton.titleLabel?.font = self.font ?? UIFont.systemFont(ofSize: 14)
leftButton.imageView?.contentMode = .scaleAspectFit
leftButton.imageView?.clipsToBounds = true
// setup text view
textView.contentInset = .zero
textView.textContainerInset = .zero
textView.backgroundColor = .clear
textView.addObserver(self, forKeyPath: UITextViewContentSizeKeyPath, options: [.new], context: nil)
textView.font = self.font ?? UIFont.systemFont(ofSize: 14)
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
rightButton.imageEdgeInsets = .zero
rightButton.titleEdgeInsets = .zero
rightButton.contentEdgeInsets = .zero
rightButton.titleLabel?.font = self.font ?? UIFont.systemFont(ofSize: 14)
rightButton.imageView?.contentMode = .scaleAspectFit
rightButton.imageView?.clipsToBounds = true
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 showLeftButton: Bool = true {
didSet {
delegate?.wantsLayout(messageView: self)
}
}
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 {
set {
textView.textContainerInset = newValue
setNeedsLayout()
delegate?.wantsLayout(messageView: self)
}
get { return textView.textContainerInset }
}
public func setButton(icon: UIImage?, for state: UIControlState, position: ButtonPosition) {
let button: UIButton
switch position {
case .left:
button = leftButton
case .right:
button = rightButton
}
button.setImage(icon, for: state)
buttonLayoutDidChange(button: button)
}
public func setButton(title: String, for state: UIControlState, position: ButtonPosition) {
let button: UIButton
switch position {
case .left:
button = leftButton
case .right:
button = rightButton
}
button.setTitle(title, for: state)
buttonLayoutDidChange(button: button)
}
public var leftButtonTint: UIColor {
get { return leftButton.tintColor }
set {
leftButton.tintColor = newValue
leftButton.setTitleColor(newValue, for: .normal)
leftButton.imageView?.tintColor = newValue
}
}
public var rightButtonTint: UIColor {
get { return rightButton.tintColor }
set {
rightButton.tintColor = newValue
rightButton.setTitleColor(newValue, for: .normal)
rightButton.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, position: ButtonPosition) {
let button: UIButton
switch position {
case .left:
button = leftButton
leftButtonAction = action
case .right:
button = rightButton
rightButtonAction = action
}
button.addTarget(target, action: action, for: .touchUpInside)
}
public override var keyCommands: [UIKeyCommand]? {
guard let action = rightButtonAction else { return nil }
return [UIKeyCommand(input: "\r", modifierFlags: .command, action: action)]
}
public func setButton(inset: CGFloat, position: ButtonPosition) {
switch position {
case .left:
leftButtonInset = inset
case .right:
rightButtonInset = inset
}
setNeedsLayout()
}
public func setButton(font: UIFont, position: ButtonPosition) {
let button: UIButton
switch position {
case .left:
button = leftButton
case .right:
button = rightButton
}
button.titleLabel?.font = font
buttonLayoutDidChange(button: button)
}
// 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 leftButtonSize = leftButton.bounds.size
let rightButtonSize = rightButton.bounds.size
let textViewY = safeBounds.minY
let textViewHeight = self.textViewHeight
let textViewMaxY = textViewY + textViewHeight
// adjust by bottom offset so content is flush w/ text view
let leftButtonFrame = CGRect(
x: safeBounds.minX + inset.left,
y: textViewMaxY - leftButtonSize.height + leftButton.bottomHeightOffset - inset.bottom,
width: leftButtonSize.width,
height: leftButtonSize.height
)
leftButton.frame = showLeftButton ? leftButtonFrame : .zero
let textViewFrame = CGRect(
x: leftButtonFrame.maxX + leftButtonInset,
y: textViewY,
width: safeBounds.width - leftButtonFrame.maxX - rightButtonSize.width - rightButtonInset - inset.right,
height: textViewHeight
)
textView.frame = textViewFrame
// adjust by bottom offset so content is flush w/ text view
let rightButtonFrame = CGRect(
x: textViewFrame.maxX + rightButtonInset,
y: textViewMaxY - rightButtonSize.height + rightButton.bottomHeightOffset - inset.bottom,
width: rightButtonSize.width,
height: rightButtonSize.height
)
rightButton.frame = rightButtonFrame
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 textViewHeight + (contentView?.bounds.height ?? 0)
}
internal var textViewHeight: CGFloat {
return ceil(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
rightButton.isEnabled = !isEmpty
rightButton.alpha = isEmpty ? 0.25 : 1
}
internal func buttonLayoutDidChange(button: UIButton) {
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) {}
}