From ea7006ad743375855fab434f4e6e9caf4274bfb5 Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Sat, 13 May 2023 18:06:44 +0300 Subject: MessageInputView conversion --- Juick/Views/MessageInputView.swift | 47 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 Juick/Views/MessageInputView.swift (limited to 'Juick/Views/MessageInputView.swift') diff --git a/Juick/Views/MessageInputView.swift b/Juick/Views/MessageInputView.swift new file mode 100644 index 0000000..2a923e2 --- /dev/null +++ b/Juick/Views/MessageInputView.swift @@ -0,0 +1,47 @@ +// +// MessageInputView.swift +// Juick +// +// Created by Vitaly Takmazov on 13.05.2023. +// Copyright © 2023 com.juick. All rights reserved. +// + +import UIKit + +@objc protocol MessageInputDelegate { + @objc func textSent(_ text: String) +} + +@objc +class MessageInputView: UIVisualEffectView, UITextViewDelegate { + @IBOutlet weak var bottomConstraint: NSLayoutConstraint! + @IBOutlet weak var textHeightConstraint: NSLayoutConstraint! + @IBOutlet weak var topMarginConstraint: NSLayoutConstraint! + @IBOutlet weak var textView: UITextView! + @IBOutlet weak var sendButton: UIButton! + + @IBAction func sendPressed(_ sender: Any) { + self.delegate?.textSent(self.textView.text) + self.textView.text = nil + } + @objc var delegate: MessageInputDelegate? + + func textSize() -> CGSize { + let size = CGSizeMake(self.textView.bounds.size.width, CGFloat(Float.greatestFiniteMagnitude)) + return self.textView.sizeThatFits(size) + } + + override var intrinsicContentSize: CGSize { + var viewSize = self.textSize() + viewSize.height = viewSize.height + self.bottomConstraint.constant + self.topMarginConstraint.constant + return viewSize + } + + func textViewDidChange(_ textView: UITextView) { + if (self.textSize().height != self.textHeightConstraint.constant) { + self.textHeightConstraint.constant = self.textSize().height + layoutIfNeeded() + } + self.sendButton.isEnabled = self.textView.text.count > 0 + } +} -- cgit v1.2.3