I see multiple answers suggest simply turning off scrollEnabled
. This is the best solution. I’m writing this answer to explain why it works.
UITextView
implements the intrinsicContentSize
property if scrollEnabled == NO
. The disassembly of the getter method looks like this:
- (CGSize)intrinsicContentSize {
if (self.scrollEnabled) {
return CGSizeMake(UIViewNoIntrinsicMetric, UIViewNoIntrinsicMetric);
} else {
// Calculate and return intrinsic content size based on current width.
}
}
That means you just need to make sure the width of the text view is constrained enough and then you can use the intrinsic content height (either via Auto Layout content hugging/compression resistance priorities or directly using the value during manual layout).
Unfortunately, this behavior is not documented. Apple could have easily saved us all some headaches… no need for an extra height constraint, subclassing, etc.