From eab28f356ee0bb7b1af8c31e57b621f2e131b0b5 Mon Sep 17 00:00:00 2001 From: Bartosz Dudar Date: Thu, 19 Oct 2017 18:03:16 +0200 Subject: [PATCH] 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 --- Demo/ViewController.swift | 8 ++- MessagesView/MessageEditorTextView.swift | 6 ++ MessagesView/MessagesToolbarContentView.swift | 9 ++- MessagesView/MessagesToolbarContentView.xib | 7 ++- MessagesView/MessagesView.swift | 56 ++++++++++++------- MessagesView/MessagesViewSettings.swift | 6 ++ 6 files changed, 66 insertions(+), 26 deletions(-) diff --git a/Demo/ViewController.swift b/Demo/ViewController.swift index ca00b19..916c19c 100644 --- a/Demo/ViewController.swift +++ b/Demo/ViewController.swift @@ -42,7 +42,13 @@ extension ViewController: MessagesViewDelegate { } func didTapRightButton() { - let text = messagesView.inputText + + let text = messagesView.inputText.trimmingCharacters(in: .whitespaces) + + guard !text.isEmpty else { + return + } + TestData.exampleMessageText.append(text) messagesView.refresh(scrollToLastMessage: true, animateLastMessage: true) } diff --git a/MessagesView/MessageEditorTextView.swift b/MessagesView/MessageEditorTextView.swift index 284c208..7cc24fe 100644 --- a/MessagesView/MessageEditorTextView.swift +++ b/MessagesView/MessageEditorTextView.swift @@ -11,11 +11,17 @@ import UIKit class MessageEditorTextView: UITextField { func applySettings(settings: MessagesViewSettings) { + textColor = settings.textInputFieldTextColor backgroundColor = settings.textInputFieldBackgroundColor tintColor = settings.textInputTintColor layer.cornerRadius = settings.textInputFieldCornerRadius placeholder = settings.textInputFieldTextPlaceholderText + + keyboardType = settings.keyboardType + keyboardAppearance = settings.keyboardAppearance + returnKeyType = settings.returnKeyType + enablesReturnKeyAutomatically = settings.enablesReturnKeyAutomatically } } diff --git a/MessagesView/MessagesToolbarContentView.swift b/MessagesView/MessagesToolbarContentView.swift index d3377f5..2f39493 100644 --- a/MessagesView/MessagesToolbarContentView.swift +++ b/MessagesView/MessagesToolbarContentView.swift @@ -34,8 +34,8 @@ class MessagesToolbarContentView: UIView { private var leftButtonEnabled: Bool = true private var rightButtonEnabled: Bool = true - var leftButtonAction: (Void) -> () = {} - var rightButtonAction: (Void) -> () = {} + var leftButtonAction: () -> () = {} + var rightButtonAction: () -> () = {} private var leftTintColor: UIColor { return leftButtonEnabled ? settings.leftButtonTextColor : settings.leftButtonDisabledColor @@ -196,6 +196,11 @@ extension MessagesToolbarContentView : UITextFieldDelegate { } func textFieldShouldReturn(_ textField: UITextField) -> Bool { + + if settings.shouldDoRightActionWithReturnKey { + didPressRightButton(textField) + } + return true } diff --git a/MessagesView/MessagesToolbarContentView.xib b/MessagesView/MessagesToolbarContentView.xib index 1f18889..9138c15 100644 --- a/MessagesView/MessagesToolbarContentView.xib +++ b/MessagesView/MessagesToolbarContentView.xib @@ -1,10 +1,11 @@ - - + + - + + diff --git a/MessagesView/MessagesView.swift b/MessagesView/MessagesView.swift index f82efa4..28001a3 100644 --- a/MessagesView/MessagesView.swift +++ b/MessagesView/MessagesView.swift @@ -44,6 +44,7 @@ public class MessagesView: UIView { @IBOutlet weak var messageInputToolbarBottomConstraint: NSLayoutConstraint! private var toolBarBottomConstraintWithoutKeyboard: CGFloat = 0 + private var toolBarFrameWithoutKeyboard: CGRect = .zero //MARK:- Public properties @@ -97,6 +98,8 @@ public class MessagesView: UIView { var bubbleImageLeft: BubbleImage = BubbleImage(cornerRadius: 8) var bubbleImageRight: BubbleImage = BubbleImage(cornerRadius: 8).flipped + private var isKeyboardShown = false + public func setBubbleImagesWith(left: BubbleImage, right: BubbleImage? = nil) { bubbleImageLeft = left @@ -207,49 +210,62 @@ public class MessagesView: UIView { NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow), name: .UIKeyboardWillShow, object: nil) NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillHide), name: .UIKeyboardWillHide, object: nil) + NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillChangeFrame), name: .UIKeyboardWillChangeFrame, object: nil) } @objc private func keyboardWillShow(notification: Notification) { - guard settings.shouldAdjustToKeyboard, - let userInfo = notification.userInfo, - let keyboardFrame = (userInfo[UIKeyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue, - let animationDuration = (userInfo[UIKeyboardAnimationDurationUserInfoKey] as? NSNumber)?.doubleValue else { + guard !isKeyboardShown else { return } - toolBarBottomConstraintWithoutKeyboard = messageInputToolbarBottomConstraint.constant + isKeyboardShown = true - let toolbarFrameInWindow = convert(messagesInputToolbar.frame, to: nil) + toolBarBottomConstraintWithoutKeyboard = messageInputToolbarBottomConstraint.constant + toolBarFrameWithoutKeyboard = convert(messagesInputToolbar.frame, to: nil) - let keyboardOverlap = toolbarFrameInWindow.origin.y - keyboardFrame.origin.y + respondToKeyboardFrameChange(notification: notification) + } + + @objc private func keyboardWillChangeFrame(notification: Notification) { - guard keyboardOverlap > 0 else { + guard isKeyboardShown else { return } - let verticalAdjusttment = keyboardOverlap + toolbarFrameInWindow.size.height - - messageInputToolbarBottomConstraint.constant = toolBarBottomConstraintWithoutKeyboard + verticalAdjusttment - - UIView.animate(withDuration: animationDuration) { - let contentOffset = self.messagesCollectionView.contentOffset - - self.messagesCollectionView.contentOffset = CGPoint(x: contentOffset.x, y: contentOffset.y + verticalAdjusttment) - self.layoutIfNeeded() - } + respondToKeyboardFrameChange(notification: notification) } @objc private func keyboardWillHide(notification: Notification) { + isKeyboardShown = false + } + + private func respondToKeyboardFrameChange(notification: Notification) { + guard settings.shouldAdjustToKeyboard, - let animationDuration = (notification.userInfo?[UIKeyboardAnimationDurationUserInfoKey] as? NSNumber)?.doubleValue else { + let userInfo = notification.userInfo, + let keyboardFrame = (userInfo[UIKeyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue, + let animationDuration = (userInfo[UIKeyboardAnimationDurationUserInfoKey] as? NSNumber)?.doubleValue else { + return + } + + let keyboardOverlap = toolBarFrameWithoutKeyboard.origin.y - keyboardFrame.origin.y + + let verticalAdjustment = keyboardOverlap > 0 ? keyboardOverlap + toolBarFrameWithoutKeyboard.size.height : 0 + + let newBottomConstraint = toolBarBottomConstraintWithoutKeyboard + verticalAdjustment + + guard newBottomConstraint != messageInputToolbarBottomConstraint.constant else { return } - messageInputToolbarBottomConstraint.constant = toolBarBottomConstraintWithoutKeyboard + messageInputToolbarBottomConstraint.constant = newBottomConstraint UIView.animate(withDuration: animationDuration) { + let contentOffset = self.messagesCollectionView.contentOffset + + self.messagesCollectionView.contentOffset = CGPoint(x: contentOffset.x, y: contentOffset.y + verticalAdjustment) self.layoutIfNeeded() } } diff --git a/MessagesView/MessagesViewSettings.swift b/MessagesView/MessagesViewSettings.swift index ef469d3..bc08252 100644 --- a/MessagesView/MessagesViewSettings.swift +++ b/MessagesView/MessagesViewSettings.swift @@ -16,6 +16,12 @@ public class MessagesViewSettings { public var leftButtonHidesKeyboard = false public var rightButtonHidesKeyboard = false public var shouldAdjustToKeyboard = true + public var shouldDoRightActionWithReturnKey = true + + public var keyboardType: UIKeyboardType = .default + public var keyboardAppearance: UIKeyboardAppearance = .default + public var returnKeyType: UIReturnKeyType = .done + public var enablesReturnKeyAutomatically = false public var textInputScrollsToRecentMessage = true