Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How do I add a tap gesture to a SwiftUI button?

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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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!

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading