Skip to content

Commit eab28f3

Browse files
committed
Fixed adjusting to changing keyboard types, added keyboard settings (type, appearance, return type key, return key auto-enabled, right action by return key), trimming and preventing empty text in demo
1 parent 8fa7b8b commit eab28f3

File tree

6 files changed

+66
-26
lines changed

6 files changed

+66
-26
lines changed

Demo/ViewController.swift

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,13 @@ extension ViewController: MessagesViewDelegate {
4242
}
4343

4444
func didTapRightButton() {
45-
let text = messagesView.inputText
45+
46+
let text = messagesView.inputText.trimmingCharacters(in: .whitespaces)
47+
48+
guard !text.isEmpty else {
49+
return
50+
}
51+
4652
TestData.exampleMessageText.append(text)
4753
messagesView.refresh(scrollToLastMessage: true, animateLastMessage: true)
4854
}

MessagesView/MessageEditorTextView.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,17 @@ import UIKit
1111
class MessageEditorTextView: UITextField {
1212

1313
func applySettings(settings: MessagesViewSettings) {
14+
1415
textColor = settings.textInputFieldTextColor
1516
backgroundColor = settings.textInputFieldBackgroundColor
1617
tintColor = settings.textInputTintColor
1718
layer.cornerRadius = settings.textInputFieldCornerRadius
1819
placeholder = settings.textInputFieldTextPlaceholderText
20+
21+
keyboardType = settings.keyboardType
22+
keyboardAppearance = settings.keyboardAppearance
23+
returnKeyType = settings.returnKeyType
24+
enablesReturnKeyAutomatically = settings.enablesReturnKeyAutomatically
1925
}
2026

2127
}

MessagesView/MessagesToolbarContentView.swift

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ class MessagesToolbarContentView: UIView {
3434
private var leftButtonEnabled: Bool = true
3535
private var rightButtonEnabled: Bool = true
3636

37-
var leftButtonAction: (Void) -> () = {}
38-
var rightButtonAction: (Void) -> () = {}
37+
var leftButtonAction: () -> () = {}
38+
var rightButtonAction: () -> () = {}
3939

4040
private var leftTintColor: UIColor {
4141
return leftButtonEnabled ? settings.leftButtonTextColor : settings.leftButtonDisabledColor
@@ -196,6 +196,11 @@ extension MessagesToolbarContentView : UITextFieldDelegate {
196196
}
197197

198198
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
199+
200+
if settings.shouldDoRightActionWithReturnKey {
201+
didPressRightButton(textField)
202+
}
203+
199204
return true
200205
}
201206

MessagesView/MessagesToolbarContentView.xib

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
2-
<document type="com.apple.InterfaceBuilder3.CocoaTouch.XIB" version="3.0" toolsVersion="11762" systemVersion="15G1217" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" colorMatched="YES">
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<document type="com.apple.InterfaceBuilder3.CocoaTouch.XIB" version="3.0" toolsVersion="13196" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" colorMatched="YES">
33
<device id="retina4_7" orientation="portrait">
44
<adaptation id="fullscreen"/>
55
</device>
66
<dependencies>
7-
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="11757"/>
7+
<deployment identifier="iOS"/>
8+
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="13173"/>
89
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
910
</dependencies>
1011
<objects>

MessagesView/MessagesView.swift

Lines changed: 36 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ public class MessagesView: UIView {
4444
@IBOutlet weak var messageInputToolbarBottomConstraint: NSLayoutConstraint!
4545

4646
private var toolBarBottomConstraintWithoutKeyboard: CGFloat = 0
47+
private var toolBarFrameWithoutKeyboard: CGRect = .zero
4748

4849
//MARK:- Public properties
4950

@@ -97,6 +98,8 @@ public class MessagesView: UIView {
9798
var bubbleImageLeft: BubbleImage = BubbleImage(cornerRadius: 8)
9899
var bubbleImageRight: BubbleImage = BubbleImage(cornerRadius: 8).flipped
99100

101+
private var isKeyboardShown = false
102+
100103
public func setBubbleImagesWith(left: BubbleImage, right: BubbleImage? = nil) {
101104

102105
bubbleImageLeft = left
@@ -207,49 +210,62 @@ public class MessagesView: UIView {
207210

208211
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow), name: .UIKeyboardWillShow, object: nil)
209212
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillHide), name: .UIKeyboardWillHide, object: nil)
213+
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillChangeFrame), name: .UIKeyboardWillChangeFrame, object: nil)
210214
}
211215

212216
@objc private func keyboardWillShow(notification: Notification) {
213217

214-
guard settings.shouldAdjustToKeyboard,
215-
let userInfo = notification.userInfo,
216-
let keyboardFrame = (userInfo[UIKeyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue,
217-
let animationDuration = (userInfo[UIKeyboardAnimationDurationUserInfoKey] as? NSNumber)?.doubleValue else {
218+
guard !isKeyboardShown else {
218219
return
219220
}
220221

221-
toolBarBottomConstraintWithoutKeyboard = messageInputToolbarBottomConstraint.constant
222+
isKeyboardShown = true
222223

223-
let toolbarFrameInWindow = convert(messagesInputToolbar.frame, to: nil)
224+
toolBarBottomConstraintWithoutKeyboard = messageInputToolbarBottomConstraint.constant
225+
toolBarFrameWithoutKeyboard = convert(messagesInputToolbar.frame, to: nil)
224226

225-
let keyboardOverlap = toolbarFrameInWindow.origin.y - keyboardFrame.origin.y
227+
respondToKeyboardFrameChange(notification: notification)
228+
}
229+
230+
@objc private func keyboardWillChangeFrame(notification: Notification) {
226231

227-
guard keyboardOverlap > 0 else {
232+
guard isKeyboardShown else {
228233
return
229234
}
230235

231-
let verticalAdjusttment = keyboardOverlap + toolbarFrameInWindow.size.height
232-
233-
messageInputToolbarBottomConstraint.constant = toolBarBottomConstraintWithoutKeyboard + verticalAdjusttment
234-
235-
UIView.animate(withDuration: animationDuration) {
236-
let contentOffset = self.messagesCollectionView.contentOffset
237-
238-
self.messagesCollectionView.contentOffset = CGPoint(x: contentOffset.x, y: contentOffset.y + verticalAdjusttment)
239-
self.layoutIfNeeded()
240-
}
236+
respondToKeyboardFrameChange(notification: notification)
241237
}
242238

243239
@objc private func keyboardWillHide(notification: Notification) {
244240

241+
isKeyboardShown = false
242+
}
243+
244+
private func respondToKeyboardFrameChange(notification: Notification) {
245+
245246
guard settings.shouldAdjustToKeyboard,
246-
let animationDuration = (notification.userInfo?[UIKeyboardAnimationDurationUserInfoKey] as? NSNumber)?.doubleValue else {
247+
let userInfo = notification.userInfo,
248+
let keyboardFrame = (userInfo[UIKeyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue,
249+
let animationDuration = (userInfo[UIKeyboardAnimationDurationUserInfoKey] as? NSNumber)?.doubleValue else {
250+
return
251+
}
252+
253+
let keyboardOverlap = toolBarFrameWithoutKeyboard.origin.y - keyboardFrame.origin.y
254+
255+
let verticalAdjustment = keyboardOverlap > 0 ? keyboardOverlap + toolBarFrameWithoutKeyboard.size.height : 0
256+
257+
let newBottomConstraint = toolBarBottomConstraintWithoutKeyboard + verticalAdjustment
258+
259+
guard newBottomConstraint != messageInputToolbarBottomConstraint.constant else {
247260
return
248261
}
249262

250-
messageInputToolbarBottomConstraint.constant = toolBarBottomConstraintWithoutKeyboard
263+
messageInputToolbarBottomConstraint.constant = newBottomConstraint
251264

252265
UIView.animate(withDuration: animationDuration) {
266+
let contentOffset = self.messagesCollectionView.contentOffset
267+
268+
self.messagesCollectionView.contentOffset = CGPoint(x: contentOffset.x, y: contentOffset.y + verticalAdjustment)
253269
self.layoutIfNeeded()
254270
}
255271
}

MessagesView/MessagesViewSettings.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,12 @@ public class MessagesViewSettings {
1616
public var leftButtonHidesKeyboard = false
1717
public var rightButtonHidesKeyboard = false
1818
public var shouldAdjustToKeyboard = true
19+
public var shouldDoRightActionWithReturnKey = true
20+
21+
public var keyboardType: UIKeyboardType = .default
22+
public var keyboardAppearance: UIKeyboardAppearance = .default
23+
public var returnKeyType: UIReturnKeyType = .done
24+
public var enablesReturnKeyAutomatically = false
1925

2026
public var textInputScrollsToRecentMessage = true
2127

0 commit comments

Comments
 (0)