/
MSGInputView.swift
132 lines (103 loc) · 3.76 KB
/
MSGInputView.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
//
// MSGInputView.swift
// MessengerKit
//
// Created by Stephen Radford on 08/06/2018.
// Copyright © 2018 Cocoon Development Ltd. All rights reserved.
//
import UIKit
/// The base class for the input view used within `MSGMessengerViewController`.
/// This can be subclassed to add custom views.
open class MSGInputView: UIControl {
/// The text view used for text input
@IBOutlet open var textView: MSGPlaceholderTextView!
/// The send button
@IBOutlet open var sendButton: UIButton!
/// The height constraint to be modified as required.
/// This should not be set manually but instead use the `minHeight` and `maxHeight` properties.
private var heightConstraint: NSLayoutConstraint!
/// The minimum height constraint to be set
public var minHeight: CGFloat = 48
/// The maximum height constraint to be set
public var maxHeight: CGFloat = 120
/// The nib the view should be loaded from.
/// If this isn't set the view will be loaded from code.
open class var nib: UINib? { return nil }
/// The style guide the input view should use.
public var style: MSGMessengerStyle? {
didSet {
guard let style = style else { return }
textView.font = style.inputFont
textView.placeholder = style.inputPlaceholder
textView.textColor = style.inputTextColor
textView.placeholderColor = style.inputPlaceholderTextColor
textView.backgroundColor = style.inputTextViewBackgroundColor
backgroundColor = style.inputViewBackgroundColor
}
}
/// The message the user sent.
/// This is stored whenever the send button is tapped.
private(set) open var message = ""
/// The text in the textView
private var text: String! {
return textView.text
}
override open var tintColor: UIColor! {
didSet {
textView.tintColor = tintColor
sendButton.tintColor = tintColor
}
}
required public override init(frame: CGRect) {
super.init(frame: frame)
setup()
}
public required init() {
super.init(frame: .zero)
setup()
}
required public init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
open override func awakeFromNib() {
super.awakeFromNib()
setup()
}
open func setup() {
addHeightConstraints()
setupTextView()
setupSendButton()
}
private func addHeightConstraints() {
heightConstraint = heightAnchor.constraint(equalToConstant: minHeight)
heightConstraint.isActive = true
}
private func setupTextView() {
textView.tintColor = tintColor
textView.delegate = self
}
private func setupSendButton() {
sendButton.isEnabled = false
sendButton.addTarget(self, action: #selector(sendButtonTapped(_:)), for: .touchUpInside)
}
@objc func sendButtonTapped(_ sender: UIButton) {
message = textView.text
textView.text = nil
sendActions(for: .primaryActionTriggered)
}
@discardableResult open override func resignFirstResponder() -> Bool {
return textView.resignFirstResponder()
}
@discardableResult open override func becomeFirstResponder() -> Bool {
return textView.becomeFirstResponder()
}
}
extension MSGInputView: MSGPlaceholderTextViewDelegate {
open func textViewDidChange(_ textView: UITextView) {
sendButton.isEnabled = textView.text != ""
let size = textView.sizeThatFits(CGSize(width: textView.bounds.size.width, height: .infinity))
let height = size.height + 11
heightConstraint.constant = height < maxHeight ? height : maxHeight
sendActions(for: .valueChanged)
}
}