Is it possible to change the placeholder text color in SwiftUI.TextField
?
<input type="text" placeholder="A red placeholder text..">
Note: In most browsers, the appearance of placeholder text is a translucent or light gray color by default.
You can change the Select placeholder color by using CSS. Use id only to change the color or placeholder and if you want to change the color of the option then use the option also.
Just use . foregroundColor to change the text color of a TextField in SwiftUI.
SwiftUI doesn't support AttributedString
and there is no direct for it (yet). BUT YOU CAN build a custom placeholder with a few lines of codes like this:
Use ZStack
and implement a placeholder yourself:
var body: some View {
ZStack(alignment: .leading) {
if text.isEmpty {
Text("Placeholder")
.foregroundColor(.red)
}
TextField("", text: $text)
}
}
So you can do any customization you like to the placeholder.
You can always create your own custom View
s to use everywhere:
struct CustomTextField: View {
var placeholder: Text
@Binding var text: String
var editingChanged: (Bool)->() = { _ in }
var commit: ()->() = { }
var body: some View {
ZStack(alignment: .leading) {
if text.isEmpty { placeholder }
TextField("", text: $text, onEditingChanged: editingChanged, onCommit: commit)
}
}
}
usage (TextField
with placeholder):
struct ContentView: View {
@State var text = ""
var body: some View {
CustomTextField(
placeholder: Text("placeholder").foregroundColor(.red),
text: $text
)
}
}
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