I’m new to SwiftUI and trying to set a specific font size for the text in a TextField. I’ve tried using the font() modifier, but it doesn’t seem to be working. Here is the relevant code:
...
TextField("Enter your name", text: $name)
.font(.system(size: 16))
.padding()
.background(Color.gray.opacity(0.2))
.cornerRadius(8)
...
I’ve also tried setting the font size using the .font(.custom()) modifier, but that didn’t work either. Any ideas on what I might be doing wrong?
>Solution :
The font() modifier should work to set the font size for the text in a TextField. It’s possible that the font size is being overridden by another modifier or style applied to the TextField.
Here are a few things you can try:
Make sure that the font size is not being overridden by another modifier. Try removing other modifiers one by one to see if they are affecting the font size.
Try setting the font size using the .custom() modifier with a specific font, like this:
less
Copy code
.font(.custom("HelveticaNeue-Light", size: 16))
Try setting the font size for the TextField using a style, like this:
less
Copy code
.textFieldStyle(RoundedBorderTextFieldStyle())
.font(.system(size: 16))
The textFieldStyle() modifier applies a system style to the TextField, and you can then apply the font() modifier to the style.
I hope this helps!