Member-only story
Presenting Expandable Text Fields In SwiftUI
No more text views, no more tricky workarounds, everything works out of the box now.
Traditionally, two kinds of text input controls exist in iOS and macOS; text fields and text views. The difference between them is that a text field accepts a single line of text, while a text view provides multi-line text editing.
Although in macOS it’s possible -but not so common- to override the default behavior of the text field and disregard the single line rule using AppKit, this was never the case in iOS. But all that up until iOS 16 and macOS 13 (Ventura), where the fourth revision of SwiftUI brings a feature that the majority of the developers would call… a present. That is, text fields that can expand either horizontally or vertically, resulting to having multiple lines when the latter is applied.
The bad news with expandable text fields, also called scrollable text fields, is that they provide no backwards compatibility. We cannot use them in projects that support iOS versions lower than 16, and macOS versions lower than 13. In such cases, we still need to resort to solutions we already put in motion, such as text views.