I have some vertically misaligned text and I don't know why...
The code:
struct IBANInputView: View {
@State var securityDigits = ""
@State var bankCode = ""
@State var accountNumber = ""
var body: some View {
HStack {
Button(action: onCountryButtonTapped, label: {
Text("NL")
.foregroundColor(Color.purple)
})
TextField("00", text: $securityDigits)
.fixedSize()
TextField("BANK", text: $bankCode)
.fixedSize()
TextField("0000 0000 00", text: $accountNumber)
}
}
func onCountryButtonTapped() {
print("LOL")
}
}
How come the text is vertically misaligned and how can I fix it?
This is happening because by default your
HStack
's vertical alignment is.center
. I was able to achieve what you wanted by setting theHStack
's vertical alignment to.firstTextBaseline
and then adjusting the button's vertical alignment slightly.Here I'm saying the first text baseline of the button is the button's
.bottom
and then moving that baseline up ever so slightly. You may have to experiment with values but I found this to be the most visually correct for your use-case.It seems to me there's a bug in
Button
though by virtue of it not properly advertising the first text baseline in the button itself. This could potentially be cleaner by making a.firstTextBaseline
-awareButton
"subclass" that you can reuse elsewhere instead of making micro-adjustments everywhere.Vertical misalignment
The
Text
inside the button does that. Try to use a constant text field.Size difference
The placeholder values of
TextField
have a different size. Once you enter text it should look the same.