SwiftUI How to center Text in a Form?
Use frame
to expand the text's frame to the full width and multilineTextAlignment
to cause text (including wrapped text) to be centered.
Form {
Text("Centered text, even if wraps")
.frame(maxWidth: .infinity)
.multilineTextAlignment(.center)
}
You can wrap your text into HStack
:
Form {
Text("I am left aligned")
HStack {
Spacer()
Text("I am centered!")
Spacer()
}
}
This works.
Form {
Text("I am left aligned")
Text("I am centered!")
.frame(maxWidth: .infinity, alignment: .center)
}