In order to create a UITextView like that of the Messages app whose height grows to fits its contents (number of lines), I subclassed UITextView and customized the intrinsicContentSize like so:
override var intrinsicContentSize: CGSize {
var size = super.intrinsicContentSize
if size.height == UIView.noIntrinsicMetric {
layoutManager.glyphRange(for: textContainer)
size.height = layoutManager.usedRect(for: textContainer).height + textContainerInset.top + textContainerInset.bottom
}
return size
}
As noted at WWDC, accessing layoutManager will force TextKit 1, we should instead use textLayoutManager. How can this code be migrated to support TextKit 2?
Does usageBoundsForTextContainer help? You can use the following code to retrieve the usage bounds for the text container:
textLayoutManager.ensureLayout(for: textLayoutManager.documentRange)
let rect = textLayoutManager.usageBoundsForTextContainer
Best,
——
Ziqiao Chen
Worldwide Developer Relations.