Skip to content

Instantly share code, notes, and snippets.

@Bogidon
Last active February 15, 2021 20:06
Show Gist options
  • Star 10 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Bogidon/cc0c9ae6f041413c39fb0ff146ad1b18 to your computer and use it in GitHub Desktop.
Save Bogidon/cc0c9ae6f041413c39fb0ff146ad1b18 to your computer and use it in GitHub Desktop.
A UITextView subclass that grows with its text but allows scrolling according to AutoLayout constraints. Updates intrinsicContentSize. For an animatable version see https://gist.github.com/Bogidon/632e265b784ef978d5d8c0b86858c2ee
//
// GrowingTextView.swift
// https://gist.github.com/Bogidon/cc0c9ae6f041413c39fb0ff146ad1b18
//
// Created by Bogdan Vitoc on 02/22/2017.
// Distributed under the MIT License: https://gist.github.com/Bogidon/cc0c9ae6f041413c39fb0ff146ad1b18#file-license
//
import UIKit
/**
A subclass of `UITextView` that grows with its text without completely disabling scrolling. It does this by updating
`intrinsicContentSize`. This means you can set inequality constraints so the text is only scrollable under certain
conditions. (e.g. you can set a maximum with a ≤ constraint).
- Note:
See [this gist](https://gist.github.com/Bogidon/632e265b784ef978d5d8c0b86858c2ee) for an alternate version that can animate.
*/
@IBDesignable
class GrowingTextView: UITextView {
override init(frame: CGRect, textContainer: NSTextContainer?) {
super.init(frame: frame, textContainer: textContainer)
commonInit()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
commonInit()
}
private func commonInit() {
NotificationCenter.default.addObserver(self, selector: #selector(scrollToBottom), name: .UITextViewTextDidChange, object: nil)
}
override var contentSize: CGSize {
didSet {
invalidateIntrinsicContentSize()
// This eliminates a weird UI glitch where inserting a new line sometimes causes there to be a
// content offset when self.bounds == self.contentSize causing the text at the top to be snipped
// and a gap at the bottom.
setNeedsLayout()
layoutIfNeeded()
}
}
override var intrinsicContentSize: CGSize {
let width = super.intrinsicContentSize.width
return CGSize(width: width, height: contentSize.height)
}
@objc override func scrollToBottom() {
// This needs to happen so the superview updates the bounds of this text view from its new intrinsicContentSize
// If not called, the bounds will be smaller than the contentSize at this moment, causing the guard to not be triggered.
superview?.layoutIfNeeded()
// Prevent scrolling if the textview is large enough to show all its content. Otherwise there is a jump.
guard contentSize.height > bounds.size.height else {
return
}
let offsetY = (contentSize.height + contentInset.top) - bounds.size.height
UIView.animate(withDuration: 0.125) {
self.setContentOffset(CGPoint(x: 0, y: offsetY), animated: false)
}
}
}
(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.
@ts95
Copy link

ts95 commented Jun 17, 2018

Lines 42 & 43 cause a stack overflow due to infinite recursion. If I remove those lines it kind of works, but with the content offset glitch.

Also, there's a superfluous override keyword on line 52.

@krypt-lynx
Copy link

scrollToBottom based on false assumption user types to the end of text
If user will scroll to top of TextView and will try to type scroll will work incorrect

Anyway, good work

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