125 - Create A Scrollable Text Field in SwiftUI

SwiftUI provides a variant of the TextField initializer that allows the creation of a scrollable text field. This initializer is especially useful when you need to display or edit a large amount of text that doesn’t fit into the available space.

Here’s an example of how to create a scrollable text field in SwiftUI:

struct ContentView: View {
  @State private var text = "A long string of text that goes on and on and on and on and on and on and on and on..."

  var body: some View {
    TextField("Enter text here", text: $text, axis: .vertical)
      .padding()
  }
}

Here’s what the preview looks like:

A scrollable text field in SwiftUI.

In this code:

With this initializer, you can create text fields that scroll their content when the text doesn’t fit in the available space, thereby providing a more user-friendly text editing experience in your SwiftUI apps.