Skip to content

enable multiline textfield to show more lines and limit to 1000 characters maximum input. #69

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jan 20, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 56 additions & 6 deletions Antidote/ChatInputView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ private struct Constants {
static let CameraHorizontalOffset: CGFloat = 10.0
static let CameraBottomOffset: CGFloat = -10.0
static let TextViewMinHeight: CGFloat = 35.0
static let MIN_MYHEIGHT: CGFloat = 45
static let MAX_MYHEIGHT: CGFloat = 90
static let MARGIN_MYHEIGHT: CGFloat = 5
static let MAX_TEXT_INPUT_CHARS = 1000
}

protocol ChatInputViewDelegate: class {
Expand Down Expand Up @@ -48,6 +52,8 @@ class ChatInputView: UIView {
fileprivate var cameraButton: UIButton!
fileprivate var textView: UITextView!
fileprivate var sendButton: UIButton!
fileprivate var myHeight: Constraint!
fileprivate var didconstraint = 0

init(theme: Theme) {
self.maxHeight = 0.0
Expand Down Expand Up @@ -82,17 +88,38 @@ extension ChatInputView {

@objc func sendButtonPressed() {
delegate?.chatInputViewSendButtonPressed(self)
updateTextviewHeight(textView)
}
}

extension ChatInputView: UITextViewDelegate {
func textViewDidChange(_ textView: UITextView) {
updateViews()
updateTextviewHeight(textView)
delegate?.chatInputViewTextDidChange(self)
}

override func didMoveToWindow() {
updateTextviewHeight(textView)
}

func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {
// get the current text, or use an empty string if that failed
let currentText = textView.text ?? ""

// attempt to read the range they are trying to change, or exit if we can't
guard let stringRange = Range(range, in: currentText) else { return false }

// add their new text to the existing text
let updatedText = currentText.replacingCharacters(in: stringRange, with: text)

// make sure the result is under MAX_TEXT_INPUT_CHARS characters
return updatedText.count <= Constants.MAX_TEXT_INPUT_CHARS
}
}

private extension ChatInputView {

func createViews(_ theme: Theme) {
topBorder = UIView()
topBorder.backgroundColor = theme.colorForType(.SeparatorsAndBorders)
Expand All @@ -116,6 +143,8 @@ private extension ChatInputView {
textView.layer.borderColor = theme.colorForType(.SeparatorsAndBorders).cgColor
textView.layer.masksToBounds = true
textView.setContentHuggingPriority(UILayoutPriority(rawValue: 0.0), for: .horizontal)
textView.autocapitalizationType = .none

addSubview(textView)

sendButton = UIButton(type: .system)
Expand Down Expand Up @@ -151,16 +180,37 @@ private extension ChatInputView {
}
}

func updateViews() {
let size = textView.sizeThatFits(CGSize(width: textView.frame.size.width, height: CGFloat.greatestFiniteMagnitude))
func updateTextviewHeight(_ t : UITextView)
{
if (self.didconstraint == 1)
{
self.myHeight.uninstall()
self.didconstraint = 0
}

if maxHeight > 0.0 {
textView.isScrollEnabled = (size.height + 2 * Constants.Offset) > maxHeight
let text_needs_size = t.sizeThatFits(
CGSize(width: t.frame.size.width,
height: CGFloat.greatestFiniteMagnitude))
var new_height = text_needs_size.height + Constants.MARGIN_MYHEIGHT
if (text_needs_size.height > Constants.MAX_MYHEIGHT)
{
new_height = Constants.MAX_MYHEIGHT
}
else {
textView.isScrollEnabled = false
else if (text_needs_size.height < Constants.MIN_MYHEIGHT) {
new_height = Constants.MIN_MYHEIGHT
}

if (self.didconstraint == 0) {
self.didconstraint = 1
self.snp.makeConstraints {
self.myHeight = $0.height.equalTo(new_height).constraint
}
}
}

func updateViews() {
textView.isScrollEnabled = true
textView.autocapitalizationType = .none
cameraButton.isEnabled = cameraButtonEnabled
sendButton.isEnabled = !textView.text.isEmpty
}
Expand Down