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

Post not yet marked as solved Up vote post of DaleOne Down vote post of DaleOne
3.0k views

Replies

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
        }
    }
}
  • Thank you so much for posting this solution, @DaleOne... and @winshy too. The FAQ and Mission text views in my app had weird spacing issues all over the place after upgrading to iOS 16 and I couldn't understand why on Earth they did. Turns out I had commented out both their layout manager lines (as I wasn't using them and Xcode was giving me warnings as a result), but I had no idea such an easy solution as uncommenting them both would fix the spacing issues. What a result. Thanks again both.

  • Wow thank you so much, and @winshy, this new behavior causes significant breaking changes! When you create a new line on a uitextview and use sizethatfits, it does not give the proper size of the textview!

Add a Comment