skip to Main Content

I want to change the format, color, for font-style of the input font shown inside textfields as a user enters their information..

I assume this would be a modifier to the TextField view itself?

Thank you

struct ContentView: View {

   @State var email = ""

   var body: some View {
    TextField("email", text: $email)
    //apply modifier here??? 
    }

}

enter image description here

2

Answers


  1. Yes, modifiers work on the TextField:

    TextField("email", text: $email)
                .foregroundColor(.red)
                .font(.system(size: 24, weight: .bold, design: .default))
    
    Login or Signup to reply.
  2. If you want your views to follow a style-guide in your app, you could define viewModifier and extension like this.

    struct Primary: ViewModifier {
        private let font: Font
        private let foregroundColor: Color
        
        init(size: CGFloat, foregroundColor: Color) {
            self.font = .system(size: UIFontMetrics.default.scaledValue(for: size))
            self.foregroundColor = foregroundColor
        }
    
        init(font: Font, foregroundColor: Color) {
            self.font = font
            self.foregroundColor = foregroundColor
        }
    
        public func body(content: Content) -> some View {
            content
                .font(font)
                .foregroundColor(foregroundColor)
        }
    }
    
    extension View {
        func primary() -> some View {
            ModifiedContent(content: self, modifier: Primary(font: .system(size: 13, weight: .semibold, design: .default), foregroundColor: .blue))
        }
    }
    

    And then use it like this

    struct SomeView: View {
        var body: some View {
            Text("Description")
                .primary()
        }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search