TextField
To read text input from user, we can use TextField. To use TextField, we need a placeholder to hold the value entered in the TextField. For that, we need to pass a binding variable. Here is a quick example:
import SwiftUI
struct ContentView: View {
@State var name: String = ""
var body: some View {
TextField("Enter your name", text: $name)
.padding()
Text("Hello, \(name)")
}
}
#Preview {
ContentView()
}
Output:

When user pass a name, Text view will be updated. We can customise TextField as we need like:
TextField("Enter your name", text: $name)
.textFieldStyle(.roundedBorder)
Output:

Here is another example:
TextField("Enter your name", text: $name)
.padding()
.font(.title)
.background(Color(.systemGray6))
.cornerRadius(10)
Output:

If you want to control auto correction of your text field, you can control using:
.disableAutocorrection(true)
TextEditor
If we need to use multi-line scrollable text editor, you can use TextEditor in SwiftUI.
import SwiftUI
struct ContentView: View {
@State var fullText: String = "SwiftUI is a user interface framework for building user interfaces for iOS, iPadOS, watchOS, tvOS, visionOS and macOS, developed by Apple Inc."
var body: some View {
VStack{
TextEditor(text: $fullText)
}
.padding()
}
}
#Preview {
ContentView()
}
SecureField
To get privet text or password, we can use SecureField in SwiftUI:
import SwiftUI
struct ContentView: View {
@State var password: String = ""
var body: some View {
VStack{
SecureField("Enter your password", text: $password)
}
.padding()
}
}
#Preview {
ContentView()
}
More about TextField: