TextField supports alignment for the typed text with the use of multilineTextAlignment modifier.
MultilineTextAlignment has three alignment values leading, center, trailing. Let’s see them in use with an example. We will add two more State properties to track course category and course type for the new DevTechie course.
struct TextFieldExample: View {
@State private var title: String = ""
@State private var category: String = ""
@State private var type: String = ""
var body: some View {
VStack {
Text("DevTechie Courses")
.font(.largeTitle)
VStack(alignment: .leading) {
Text("Enter new course title")
.font(.title3)
TextField("Course title", text: $title)
.textFieldStyle(.roundedBorder)
.multilineTextAlignment(.center)
TextField("Course category", text: $category)
.textFieldStyle(.roundedBorder)
.multilineTextAlignment(.trailing)
TextField("Course type", text: $type)
.textFieldStyle(.roundedBorder)
.multilineTextAlignment(.leading)
}.padding(.top, 20)
}.padding()
}
}