summaryrefslogtreecommitdiff
path: root/Juick/Views/MessageInputView.swift
blob: 2a923e29f5c8f845b3efec16ee30ca221a379afb (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
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
    }
}