Reading time: 1 min
This recipe shows how to style SwiftUI TextField's prompt / placeholder text, in order to, e.g, change its color or font. Alternatively, you can put a fully custom view for the placeholder. The end result looks like this:
Since there is no built-in way of doing this, we'll have to resort...
SwiftUI Alert with TextField
Reading time: 2 min
This recipe shows how to add a TextField
to a SwiftUI alert
dialog. The end result looks like this:
The dialog supports:
- Custom title and message.
- Text field with a placeholder and keyboard type.
- Confirm button, whose callback returns the content of the text field.
- Optiona...
Reading time: 1 min
A common issue in SwiftUI is that, when the keyboard appears, it covers up a part of your UI. This is especially problematic if it overlaps the exact TextField
you're editing, so that you can't even see what you're typing!
Take this credit card input field at the bottom of a screen:
If you o...
Focus change in TextField and SecureField
Reading time: 2 min
Detecting if a text field gained or lost focus is not entirely straightforward in SwiftUI.
TextField
constructor has a parameter named onEditingChanged
, but it triggers only when the user taps the return key in the virtual keyboard, not when the focus is actually lost.
Even worse, SecureFi...
Clip content in wrapped UITextField
Reading time: 1 min
When implementing a custom TextField
by wrapping a UITextField
in UIViewRepresentable
(just like we did in our custom SecureField that tracks focus change, you need to be careful as its size normally grows with its content. This can lead to the field spilling outside the bounds of its parent...