Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom font size for Text in SwiftUI

I have a label in my view that I want to use the system font size in medium, with a size of 21 points.
I created a custom extension to re-use the font created:

extension Font {     static var primaryButton: Font {         return Font.custom("SFUIDisplay-Light", size: 21)     } } 

However, this does not have any effect. I changed the string to HelveticaNeue-UltraLight and it did work, so I'm guessing that SFUIDisplay-Light is simply the incorrect font name.
In font book, it says SFProText-Light, but that also did not work for me.

What is the correct font name of the San Francisco font in SwiftUI?
Or: how can I set the font size using the system font?

like image 904
LinusGeffarth Avatar asked Jun 05 '19 17:06

LinusGeffarth


People also ask

How do I customize text in SwiftUI?

Hold the command key and click the text to bring up a pop-over menu. Choose Show SwiftUI Inspector and then you can edit the text/font properties.

How do I use custom fonts in SwiftUI?

To use a custom font, add the font file that contains your licensed font to your app, and then apply the font to a text view or set it as a default font within a container view. SwiftUI's adaptive text display scales the font automtically using Dynamic Type.

How do I set dynamic font size in SwiftUI?

SwiftUI comes with support for all of Dynamic Type's font sizes, all set using the . font() modifier. However, if you ask for a specific font and size, you'll find your text no longer scales up or down automatically according to the user's Dynamic Type settings – it remains fixed.


2 Answers

You can set an explicit size for the system font with:

.font(.system(size: 60)) 
like image 131
NRitH Avatar answered Oct 27 '22 08:10

NRitH


You can set custom font size like this,

.font(.custom("FONT_NAME", size: 20)) 
like image 21
Anjali Kevadiya Avatar answered Oct 27 '22 08:10

Anjali Kevadiya