Neph Muw
Neph Muw

Reputation: 910

SwiftUI: How to change placeholder font of TextField?

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

Answers (2)

Al Mustakim
Al Mustakim

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

Asperi
Asperi

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

Related Questions