Skip to content

Instantly share code, notes, and snippets.

@Bogidon
Last active January 11, 2023 07:04
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Bogidon/632e265b784ef978d5d8c0b86858c2ee to your computer and use it in GitHub Desktop.
Save Bogidon/632e265b784ef978d5d8c0b86858c2ee to your computer and use it in GitHub Desktop.
A short and understandable UITextView subclass that grows with its text. Animatable. Updates intrinsicContentSize. If you don't need animations check out https://gist.github.com/Bogidon/cc0c9ae6f041413c39fb0ff146ad1b18
//
// AnimatedGrowingTextView.swift
// https://gist.github.com/Bogidon/632e265b784ef978d5d8c0b86858c2ee
//
// Created by Bogdan Vitoc on 02/15/2017.
// Distributed under the MIT License: https://gist.github.com/Bogidon/632e265b784ef978d5d8c0b86858c2ee#file-license
//
import UIKit
/**
A subclass of `UITextView` that grows with its text. Animatable. Updates intrinsicContentSize for maximum flexibility (e.g. you can set a maximum with a ≤ constraint).
- Note:
See [this gist](https://gist.github.com/Bogidon/cc0c9ae6f041413c39fb0ff146ad1b18) for an alternate (much simpler) version that doesn't animate.
*/
@IBDesignable
class GrowingTextView: UITextView {
/// Determines whether there is an animation when the intrisic size changes to match the content size. Defaults to `true`.
var isGrowingAnimated = true
/// The duration for growing the intrinsic size to the content size. Defaults to `0.2`.
var growingAnimationDuration: TimeInterval = 0.2
private var didStartChangingText = false
private lazy var oldText: String = self.textStorage.string
override var contentSize: CGSize {
didSet {
if didStartChangingText {
invalidateIntrinsicContentSize(animated: isGrowingAnimated)
} else {
// textStorage is empty the first time it's set
let isTextStorageEmpty = oldText.isEmpty
let isTextStorageSame = oldText == textStorage.string
didStartChangingText = !(isTextStorageEmpty || isTextStorageSame)
oldText = textStorage.string
if didStartChangingText {
invalidateIntrinsicContentSize(animated: isGrowingAnimated)
} else {
invalidateIntrinsicContentSize(animated: false)
}
}
}
}
/// Adds ability to animate `invalidateIntrinsicContentSize`
private func invalidateIntrinsicContentSize(animated: Bool) {
if animated {
UIView.animate(withDuration: growingAnimationDuration) {
self.invalidateIntrinsicContentSize()
self.setNeedsLayout()
self.layoutIfNeeded()
}
} else {
invalidateIntrinsicContentSize()
}
}
override var intrinsicContentSize: CGSize {
let width = super.intrinsicContentSize.width
return CGSize(width: width, height: contentSize.height)
}
}
(The MIT License)
Copyright (c) 2017 Bogdan Vitoc
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
'Software'), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@Bogidon
Copy link
Author

Bogidon commented Feb 22, 2017

WARNING: This only works under certain conditions.

The animation always grows the height of the text view from the center out in both directions. So unless your text view is centered, there will be a weird jump animation. I'm working on a fix.

In the meantime you can use the non-animated version (to which I actually added some scroll animations to keep the the text at the bottom of the text view).

@ThoseGuysInTown
Copy link

So easy to use! Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment