iOS 16 UITextView line spacing when empty

Hi,

I just discovered a weird bug with UITextView on iOS 16 beta 4.

For some reason now, when the scrolling is disabled, the intrinsic content size of the text view is considering the line spacing even when the textview is empty.

For example, in the below code we are setting a big lineSpacing of 50 to the text view typingAttributes attribute.

class ViewController: UIViewController {

    @IBOutlet weak var textView: UITextView! {
        didSet {
            //Let's set the textView typingAttributes with a lineSpacing of 50.
            var attributes = [NSAttributedString.Key: Any]()
            let paragraphStyle: NSMutableParagraphStyle = NSMutableParagraphStyle()
            paragraphStyle.lineSpacing = 50
            attributes[NSAttributedString.Key.paragraphStyle] = paragraphStyle
            attributes[NSAttributedString.Key.font] = UIFont.preferredFont(forTextStyle: .body)
            textView.typingAttributes = attributes
        }
    }
}

On previous iOS versions, everyting it's ok and the line spacing is added only when there are more than one line (see below image).

However, on iOS 16 beta 4, the line spacing is added also when the content is empty (see below image on the left). A soon as we type something the height collapse to the correct height (see below image in the center).

Is this a new expected behavior or a bug? If it is a bug, someone has found a temporary fix for that?

Thank you

After some research I found out (thanks to the following post) that in iOS 16 UITextView use Text Kit 2.

Unfortunately the new implementation has the issue described above, but it's possibile to force an UITextView to fallback to Text Kit 1 by accessing the layoutManager property (thanks to winshy for this solution).

So, in order to fix the issue you just need to add the following line:

_ = textView.layoutManager

The full code of the original example will become:

class ViewController: UIViewController {

    @IBOutlet weak var textView: UITextView! {
        didSet {

            _ = textView.layoutManager

            var attributes = [NSAttributedString.Key: Any]()
            let paragraphStyle: NSMutableParagraphStyle = NSMutableParagraphStyle()
            paragraphStyle.lineSpacing = 50
            attributes[NSAttributedString.Key.paragraphStyle] = paragraphStyle
            attributes[NSAttributedString.Key.font] = UIFont.preferredFont(forTextStyle: .body)
            textView.typingAttributes = attributes
        }
    }
}
iOS 16 UITextView line spacing when empty
 
 
Q