Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Newbie question. How to set TextField's horizontal size to N characters

Tags:

swiftui

Sorry if the question has been already answered. I'm new to Swift, and I want to create a simple fixed-size TextField for one-time password:

TextField("******", text: self.$otp)
                    .padding()
                    .textContentType(.oneTimeCode)
                    .keyboardType(.numberPad)

I was hoping to find a configuration parameter similar to <input size="6"/> in HTML so that the engine would automatically compute the TextField's size for N characters.

Is there a simple solution to this without jumping through hoops like using ZStacks and spawning N text fields as I saw some enthusiasts were suggesting?

like image 736
andbi Avatar asked Oct 25 '25 12:10

andbi


1 Answers

Use a fixed size TextField.

TextField("******", text: $text).fixedSize()
like image 182
mahan Avatar answered Oct 28 '25 04:10

mahan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!