Skip to content

Instantly share code, notes, and snippets.

@mathewsanders
Created January 3, 2017 21:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mathewsanders/bf53235608b53eff68a4f0ffb9e6d373 to your computer and use it in GitHub Desktop.
Save mathewsanders/bf53235608b53eff68a4f0ffb9e6d373 to your computer and use it in GitHub Desktop.
TextView subclass that animates resize as text is entered
import UIKit
/// TextView subclass that animates resize as text is entered
/// Should be used with constraints that pin width so that contentView
/// expands in height to accommodate the current text
class ResizingTextView: UITextView {
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
isScrollEnabled = false
NotificationCenter.default.addObserver(self, selector: #selector(textChanged), name: NSNotification.Name.UITextViewTextDidChange, object: nil)
}
func textChanged() {
UIView.animate(withDuration: 0.2, animations: {
self.invalidateIntrinsicContentSize()
self.superview?.setNeedsLayout()
self.superview?.layoutIfNeeded()
})
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment