< How to create a stepper and read values from it | How to let users select a color with ColorPicker > |
Updated for Xcode 14.2
SwiftUI has a TextEditor
view for handling multi-line, scrolling text. You can set the font, change the colors as needed, and even adjust line spacing and how many lines can be created.
You need to store the current value of your text field somewhere, using @State
or similar. For example, we could create a text view to let the user enter profile data like this:
struct ContentView: View {
@State private var profileText = "Enter your bio"
var body: some View {
NavigationStack {
TextEditor(text: $profileText)
.foregroundColor(.secondary)
.padding(.horizontal)
.navigationTitle("About you")
}
}
}
Download this as an Xcode project
For single-line text entry, use TextField
instead.
Tip: In case you were wondering, at this time there is no ability to add formatted text inside a TextEditor
view.
SAVE 50% To celebrate WWDC23, all our books and bundles are half price, so you can take your Swift knowledge further without spending big! Get the Swift Power Pack to build your iOS career faster, get the Swift Platform Pack to builds apps for macOS, watchOS, and beyond, or get the Swift Plus Pack to learn advanced design patterns, testing skills, and more.
Link copied to your pasteboard.