< How to hide the label of a Picker, Stepper, Toggle, and more using labelsHidden() | How to let users select a color with ColorPicker > |
Updated for Xcode 13.3
New in iOS 14
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 {
NavigationView {
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.
SPONSORED Fernando's book will guide you in fixing bugs in three real, open-source, downloadable apps from the App Store. Learn applied programming fundamentals by refactoring real code from published apps. Hacking with Swift readers get a $10 discount!
Sponsor Hacking with Swift and reach the world's largest Swift community!
Link copied to your pasteboard.