textfield placeholder color swift code example
Example 1: change textfield placeholder color swiftui
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)
}
}
}
struct ContentView: View {
@State var text = ""
var body: some View {
CustomTextField(
placeholder: Text("placeholder").foregroundColor(.red),
text: $text
)
}
}
Example 2: set white place holder color in swift
placeholderLabel.textColor (Swift 4)
_placeholderLabel.textColor (Swift 3 or less)
Example 3: swift uitextfield placeholder color
myTextField.attributedPlaceholder = NSAttributedString(string: "placeholder text",
attributes: [NSAttributedString.Key.foregroundColor: UIColor.white])