Created
July 20, 2020 18:24
-
-
Save Abhishek9634/2f5e452ee866a449929c8fc48da53854 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// ViewController.swift | |
// ExpandableTextViewDemo | |
// | |
// Created by Abhishek Thapliyal on 20/07/20. | |
// Copyright © 2020 Abhishek Thapliyal. All rights reserved. | |
// | |
import UIKit | |
class ViewController: UIViewController { | |
@IBOutlet weak var placeHolderLabel: UILabel! | |
@IBOutlet weak var commentTextView: UITextView! | |
@IBOutlet weak var bottomConstraint: NSLayoutConstraint! | |
@IBOutlet weak var commentConstraint: NSLayoutConstraint! | |
private let maxHeight: CGFloat = 100 | |
private let minHeight: CGFloat = 50 | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
self.setupInitials() | |
} | |
private func setupInitials() { | |
self.setupKeyboardNotifications() | |
self.setupGesture() | |
self.setupTextView() | |
} | |
@IBAction func sendCommentAction(_ sender: Any) { | |
self.handleSubmitComment() | |
} | |
private func handleSubmitComment() { | |
guard let text = self.commentTextView.text else { return } | |
let comment = text.trimmingCharacters(in: .whitespacesAndNewlines) | |
if comment.isEmpty { return } | |
self.commentConstraint.constant = self.minHeight | |
UIView.animate(withDuration: 0.1, animations: { | |
self.view.layoutIfNeeded() | |
}) { _ in | |
self.commentTextView.text = nil | |
self.commentTextView.resignFirstResponder() | |
} | |
} | |
} | |
extension ViewController { | |
private func setupKeyboardNotifications() { | |
NotificationCenter.default.addObserver(self, | |
selector: #selector(keyboardWillShow(_:)), | |
name: UIResponder.keyboardWillShowNotification, | |
object: nil) | |
NotificationCenter.default.addObserver(self, | |
selector: #selector(keyboardWillHide(_:)), | |
name: UIResponder.keyboardWillHideNotification, | |
object: nil) | |
} | |
@objc | |
private func keyboardWillShow(_ sender: Notification) { | |
guard let info = sender.userInfo, | |
let frame = (info[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue else { | |
return | |
} | |
self.bottomConstraint.constant = frame.size.height | |
UIView.animate(withDuration: 0.3) { | |
self.view.layoutIfNeeded() | |
} | |
} | |
@objc | |
private func keyboardWillHide(_ sender: Notification) { | |
self.bottomConstraint.constant = 0 | |
self.view.layoutIfNeeded() | |
} | |
} | |
extension ViewController { | |
private func setupGesture() { | |
let gesture = UITapGestureRecognizer(target: self, | |
action: #selector(handleGesture(_:))) | |
self.view.addGestureRecognizer(gesture) | |
} | |
@objc | |
func handleGesture(_ sender: UITapGestureRecognizer) { | |
self.view.endEditing(true) | |
} | |
} | |
extension ViewController: UITextViewDelegate { | |
private func setupTextView() { | |
self.commentTextView.delegate = self | |
} | |
func textViewDidEndEditing(_ textView: UITextView) { | |
self.placeHolderLabel.isHidden = !textView.text.isEmpty | |
} | |
func textViewDidChange(_ textView: UITextView) { | |
var height = self.minHeight | |
if textView.contentSize.height <= self.minHeight { | |
height = self.minHeight | |
} else if textView.contentSize.height >= self.maxHeight { | |
height = self.maxHeight | |
} else { | |
height = textView.contentSize.height | |
} | |
self.placeHolderLabel.isHidden = !textView.text.isEmpty | |
self.commentConstraint.constant = height | |
UIView.animate(withDuration: 0.1) { | |
self.view.layoutIfNeeded() | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment