Is it possible to replace every whitespace that is created by pressing the spacebar with another character, which in this case is an underscore, while the user is typing in the text field?
I found something about
.replacingOccurrences(of: "", with: "_")
but don’t know how to apply it in real-time while typing is occurring.
Here is the text field I am using with a simple state variable:
TextField("Enter a username", text: $txt)
.padding()
.disableAutocorrection(true)
.autocapitalization(.none)
2
Answers
The simplest version of this is to listen to changes of
txt
withonChange
:You’ll likely want a more robust solution for finding whitespace, so you may want to apply some of the solutions to finding whitespace characters here or here with this
onChange
method.and also you can show your text Field and Text like this