Skip to content

Commit

Permalink
Fixed adjusting to changing keyboard types, added keyboard settings (…
Browse files Browse the repository at this point in the history
…type, appearance, return type key, return key auto-enabled, right action by return key), trimming and preventing empty text in demo
  • Loading branch information
bdudarPGS committed Oct 19, 2017
1 parent 8fa7b8b commit eab28f3
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 26 deletions.
8 changes: 7 additions & 1 deletion Demo/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
6 changes: 6 additions & 0 deletions MessagesView/MessageEditorTextView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

}
9 changes: 7 additions & 2 deletions MessagesView/MessagesToolbarContentView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -196,6 +196,11 @@ extension MessagesToolbarContentView : UITextFieldDelegate {
}

func textFieldShouldReturn(_ textField: UITextField) -> Bool {

if settings.shouldDoRightActionWithReturnKey {
didPressRightButton(textField)
}

return true
}

Expand Down
7 changes: 4 additions & 3 deletions MessagesView/MessagesToolbarContentView.xib
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<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">
<?xml version="1.0" encoding="UTF-8"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.XIB" version="3.0" toolsVersion="13196" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" colorMatched="YES">
<device id="retina4_7" orientation="portrait">
<adaptation id="fullscreen"/>
</device>
<dependencies>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="11757"/>
<deployment identifier="iOS"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="13173"/>
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
</dependencies>
<objects>
Expand Down
56 changes: 36 additions & 20 deletions MessagesView/MessagesView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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()
}
}
Expand Down
6 changes: 6 additions & 0 deletions MessagesView/MessagesViewSettings.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit eab28f3

Please sign in to comment.