I would like to use a specific TextField for url entries where the user could only enter lowercase characters, but I can't find any TextField modifier for this purpose. Is there any solution?
SwiftUI's TextField view normally lets users write their text in whatever case they want, but if you want to control that you can force either uppercase or lowercase text using the textCase() modifier. Important: If you're using Xcode 12 you need to use RoundedBorderTextFieldStyle() rather than .
You just make a View with all the code you want for the SecureTextField and the TextField then all you have to do is call the HybridTextField where ever you need it.
SwiftUI's TextField supports placeholder text just like UITextField did – gray text that is shown in the text field when it's empty, either giving users a prompt (“Enter your password”) or showing some example data.
TextField has a .autocapitalization()
method.
You can use like this without custom binding:
TextField("URL", text: $url) .keyboardType(.URL) .autocapitalization(.none)
For iOS 15 SwiftUI have a new .textInputAutocapitalization()
method:
.textInputAutocapitalization(.never)
This means that any text input by the user will be .lowercased()
You can create a custom binding and set your state URL variable to the lowercased version of the input through it:
struct ContentView: View { @State var url: String = "" var body: some View { let binding = Binding<String>(get: { self.url }, set: { self.url = $0.lowercased() }) return VStack { TextField("Enter URL", text: binding) } } }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With