SwiftUI. How to change the placeholder color of the TextField?
There is no api for it (yet). BUT YOU CAN:
Use a custom placeholder
modifier to show any view as the holder of any other view! e.g:
TextField("", text: $text)
.placeholder(when: text.isEmpty) {
Text("Placeholder recreated").foregroundColor(.gray)
}
It's a simple ZStack
that you can in a View
extension like:
extension View {
func placeholder<Content: View>(
when shouldShow: Bool,
alignment: Alignment = .leading,
@ViewBuilder placeholder: () -> Content) -> some View {
ZStack(alignment: alignment) {
placeholder().opacity(shouldShow ? 1 : 0)
self
}
}
}
Now you can apply any kind of style to the placeholder like this gradient placeholder with image:
✅ If you are interested, Here is how to apply resizable gradient on any view
The Art of the simplicity
Most of the time you need to pass just a string and a gray placeholder like:
TextField("", text: $text)
.placeholder("Placeholder", when: text.isEmpty)
you can write a simple wrapper around the above extension for it:
extension View {
func placeholder(
_ text: String,
when shouldShow: Bool,
alignment: Alignment = .leading) -> some View {
placeholder(when: shouldShow, alignment: alignment) { Text(text).foregroundColor(.gray) }
}
}
Just like that /h4> How Can I Adjust TextField Placeholder Color : SwiftUI
You cannot change the default color of the placeholder yet. However, you can achieve it by writing a custom TextField
using either UITextField
or UITextView
. Here is an example where I created a custom TextArea
View using UITextView
where I can set custom color to the placeholder. See my comment below in makeUIView(_:)
struct TextArea: UIViewRepresentable {
@State var placeholder: String
@Binding var text: String
func makeCoordinator() -> Coordinator {
Coordinator(self, placeholder: placeholder)
}
func makeUIView(context: Context) -> UITextView {
let textView = UITextView()
textView.text = placeholder
// Here you can set the color for placeholder text as per your choice.
textView.textColor = .lightGray
textView.delegate = context.coordinator
return textView
}
func updateUIView(_ textView: UITextView, context: Context) {
if !text.isEmpty {
textView.text = text
textView.textColor = .black
}
}
class Coordinator: NSObject, UITextViewDelegate {
var textArea: TextArea
var placeholder: String
init(_ textArea: TextArea, placeholder: String) {
self.textArea = textArea
self.placeholder = placeholder
}
func textViewDidBeginEditing(_ textView: UITextView) {
if textView.textColor == .lightGray {
textView.text = nil
textView.textColor = .black
}
}
func textViewDidEndEditing(_ textView: UITextView) {
if textView.text.isEmpty {
textView.text = placeholder
textView.textColor = UIColor.lightGray
}
}
}
}
EDIT:
The above text area can be used in the view like this:
TextArea(placeholder: textValue, text: $textValue)
How can I change fontWeight of a placeholder in SwiftUI?
for TextField
you use
.font(Font.custom("Life Savers", size: 26).weight(.heavy))
Changing Placeholder Text Color with Swift
You can set the placeholder text using an attributed string. Just pass the color you want to the attributes
parameter.
Swift 5:
let myTextField = UITextField(frame: CGRect(x: 0, y: 0, width: 200, height: 30))
myTextField.backgroundColor = .blue
myTextField.attributedPlaceholder = NSAttributedString(
string: "Placeholder Text",
attributes: [NSAttributedString.Key.foregroundColor: UIColor.white]
)
Swift 3:
myTextField.attributedPlaceholder = NSAttributedString(
string: "Placeholder Text",
attributes: [NSAttributedStringKey.foregroundColor: UIColor.white]
)
Older Swift:
myTextField.attributedPlaceholder = NSAttributedString(
string: "Placeholder Text",
attributes: [NSForegroundColorAttributeName: UIColor.white]
)
SwiftUI - How do I change the text color of a Textfield?
.foregroundColor
actually does change the text color of TextField
but only when it has a default value, for example this will work fine:
TextField(.constant("Hello World"), placeholder: Text("Type here..."))
.foregroundColor(.green)
But once you remove the whole text, text field loses not only its color but the rest of its modifiers such as its alignment as well. This is likely a bug in the current version so I'll file a bug report.
Update: This issue was fixed with Xcode 11 beta 3.
SwiftUI textfield color
Find below a demo of some possible approach. Also might worth considering representable around UITextField
as it is much more configurable.
Tested with Xcode 11.7 / iOS 13.7
struct PlaceholderTextFieldStyle: TextFieldStyle {
let placeholder: String
@Binding var text: String
init(_ placeholder: String, text: Binding<String>) {
self.placeholder = placeholder
self._text = text
}
func _body(configuration: TextField<Self._Label>) -> some View {
ZStack {
if text.isEmpty {
Text(placeholder)
}
configuration
}.foregroundColor(.white)
}
}
struct DemoView: View {
@State private var pass = ""
var body: some View {
SecureField("", text: $pass)
.textFieldStyle(PlaceholderTextFieldStyle("Password", text: $pass))
.padding(.all).border(Color.white).cornerRadius(3.0).padding(.horizontal)
.padding()
.background(Color.blue)
}
}
change color of uitextfield not working in swiftui
I want to keep it always black.
Use .black color instead
TextField(self.textFieldTitle ?? "", text: self.$textFieldInput, onEditingChanged: { editing in self.dosomeCode()}, onCommit: { self.someCode()})
.background(Color.green)
.foregroundColor(Color.black) // << here !!
Tested with Xcode 13.2 / iOS 15.2
Related Topics
Wkwebview Does Not Load Links to Pdfs
Swift Sphere Combine Star Data
Alamofire Asynchronous Completionhandler For Json Request
Include Swiftui Views in Existing Uikit Application
Adding 3D Object to Argeoanchor
Take a Snapshot of Current Screen with Metal in Swift
Generic Swift 4 Enum With Void Associated Type
Ios13 Navigation Bar Large Titles Not Covering Status Bar
Retrieve Only 5 Users At a Time :Firebase [Like Instagram]
Get the Top Viewcontroller in iOS Swift
Firebase Snapshot.Key Not Returning Actual Key
Can You Override Between Extensions in Swift or Not? (Compiler Seems Confused!)
Rounding in Swift with Round()