Reputation: 910
TextField(Text("Placeholder").font(.custom("font-name", size: 22)), $text)
How to change placeholder font of TextField?
Maybe to use some attributed string.
I have tried to wrap it as a Text
but no luck, got next error:
Initializer 'init(_:text:onEditingChanged:onCommit:)' requires that 'Text' conform to 'StringProtocol'
Upvotes: 1
Views: 2593
Reputation: 569
1. Simply you can Use this:
TextField("", text: $text, prompt: Text("Placeholder").font(.custom("font-name", size: 22)))
2. You may Use your custom placeholder :
Make your placeholder:
public struct PlaceholderStyle: ViewModifier {
var showPlaceHolder: Bool
var placeholder: String
public func body(content: Content) -> some View {
ZStack(alignment: .leading) {
if showPlaceHolder {
Text(placeholder)
.padding(.horizontal, 15)
}
content
.foregroundColor(Color.white)
.padding(5.0)
}
}
}
Usage :
TextField("", text: $data)
.modifier(PlaceholderStyle(showPlaceHolder: data.isEmpty,
placeholder: "My Placeholder"))
Upvotes: 3
Reputation: 258117
It can be done with different constructor, using prompt
, like
TextField("", text: $text, prompt: Text("Placeholder").font(.custom("font-name", size: 22)))
// or
TextField(text: $text, prompt: Text("Placeholder").font(.custom("font-name", size: 22))) {
Text("Label here or empty view")
}
Upvotes: 0