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

Viewed 79

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?

2 Answers

Use a fixed size TextField.

TextField("******", text: $text).fixedSize()

you can use frame for such tasks:

TextField(title, text: binding).frame(height: 40)

or

TextField(title, text: binding).frame(width: 40)
Related