UITextView
dynamic height
iOS development
Swift
auto-resizing

how to make UITextView height dynamic according to text length?

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

A UITextView can display multiple lines of text, but it does not automatically grow in height the way many developers expect. If scrolling stays enabled, the text view usually keeps a fixed frame and scrolls internally instead of expanding.

To make the height track the text length, the usual pattern is to disable scrolling, let Auto Layout handle width, and update a height constraint or layout pass whenever the text changes. The exact setup depends on whether the view is editable, read-only, or embedded in a dynamic cell.

The Basic Rule: Disable Internal Scrolling

If isScrollEnabled is true, the text view prefers scrolling over resizing. So the first step is usually:

swift
textView.isScrollEnabled = false

This tells the control that growing vertically is allowed.

If the text view lives in an Auto Layout hierarchy with a fixed width, UIKit can then compute a more useful height.

Update a Height Constraint With sizeThatFits

A common and reliable approach is to keep an outlet to the height constraint and recompute it after the text changes.

swift
1import UIKit
2
3class ViewController: UIViewController, UITextViewDelegate {
4    @IBOutlet weak var textView: UITextView!
5    @IBOutlet weak var textViewHeight: NSLayoutConstraint!
6
7    override func viewDidLoad() {
8        super.viewDidLoad()
9        textView.isScrollEnabled = false
10        textView.delegate = self
11        updateTextViewHeight()
12    }
13
14    func textViewDidChange(_ textView: UITextView) {
15        updateTextViewHeight()
16    }
17
18    private func updateTextViewHeight() {
19        let size = CGSize(width: textView.frame.width, height: .greatestFiniteMagnitude)
20        let estimatedSize = textView.sizeThatFits(size)
21        textViewHeight.constant = estimatedSize.height
22        view.layoutIfNeeded()
23    }
24}

This is one of the simplest ways to keep the control height in sync with the current content.

Make Auto Layout Work For You

For dynamic sizing to behave well, the text view must have a known width. Without a stable width, the system cannot calculate the correct wrapping height.

A typical setup is:

  • leading and trailing constraints fixed
  • top constraint fixed
  • height constraint connected as an outlet and updated in code

The layout becomes unstable when width is ambiguous or when conflicting vertical constraints are present.

Dynamic Height in Table or Collection Cells

Inside reusable cells, dynamic text views can become more complicated because the cell itself may need to remeasure.

In those cases, you usually need to:

  • disable text view scrolling
  • update the height constraint
  • trigger cell layout updates cleanly

For non-editable text content, a UILabel is often a simpler choice. Use UITextView only when you actually need text editing, selection, links, or rich text behavior.

Avoid Infinite Layout Loops

A common trap is updating layout in a way that triggers another text change or repeated constraint recalculation. Keep the update logic narrow:

  • recompute only the needed height
  • avoid unnecessary assignment if the value did not change much
  • do not call heavy layout methods repeatedly inside fast typing loops unless needed

If you see jumpy UI, inspect whether the height is being reset too often.

Read-Only Text Views Can Sometimes Use Intrinsic Sizing

If the text view is not editable and the content changes infrequently, a simpler approach may be possible by disabling scrolling and relying on layout updates after setting the text.

But for editable text views, explicit height updates are usually more predictable than hoping intrinsic sizing will always do exactly what you want.

Common Pitfalls

A common mistake is leaving isScrollEnabled enabled and expecting the text view to expand. In that mode, it usually prefers internal scrolling.

Another mistake is measuring height before the text view has its final width. Height calculations based on zero or temporary width are wrong.

Developers also use UITextView where a UILabel would be simpler and more stable for dynamic text display.

Finally, be careful inside reusable cells. Dynamic measurement there affects both the text view and the container layout, so partial updates can produce flicker or constraint warnings.

Summary

  • Disable internal scrolling if the UITextView should grow with its content.
  • Use sizeThatFits and a height constraint for predictable resizing.
  • Make sure Auto Layout provides a stable width before calculating height.
  • Prefer UILabel for read-only text when editing features are unnecessary.
  • Most dynamic-height problems come from scrolling still being enabled or from measuring before layout is ready.

Course illustration
Course illustration

All Rights Reserved.