SwiftUI forms: TextField
The first form control we’ll see is the simplest: TextField
.
This lets us show some text, like the Text
view, and it can be editable by the user, so we can get input in the form of text.
Here’s the most basic example of TextField
:
struct ContentView: View {
@State private var name = ""
var body: some View {
Form {
TextField("", text: $name)
}
}
}
We have a SwiftUI: properties that we prefaced with the @State
property wrapper.
Run the code. You can see an empty text field. You can tap on it:
And you can enter any text inside it:
The first argument of TextField
is a string that’s visualized when the field is empty. You can fill it with any text you want, like this:
TextField("Your name", text: $name)
I wrote 21 books to help you become a better developer:
- HTML Handbook
- Next.js Pages Router Handbook
- Alpine.js Handbook
- HTMX Handbook
- TypeScript Handbook
- React Handbook
- SQL Handbook
- Git Cheat Sheet
- Laravel Handbook
- Express Handbook
- Swift Handbook
- Go Handbook
- PHP Handbook
- Python Handbook
- Linux Commands Handbook
- C Handbook
- JavaScript Handbook
- Svelte Handbook
- CSS Handbook
- Node.js Handbook
- Vue Handbook
Also, JOIN MY CODING BOOTCAMP, an amazing cohort course that will be a huge step up in your coding career - covering React, Next.js - next edition February 2025