Custom font size for Text in SwiftUI

SwiftFontsSwiftui

Swift Problem Overview


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?

Swift Solutions


Solution 1 - Swift

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

.font(.system(size: 60))

Solution 2 - Swift

You can set custom font size like this,

.font(.custom("FONT_NAME", size: 20))

Solution 3 - Swift

Text("Default font design").font(Font.system(size:30, design: .default))
Text("Monospaced font design").font(Font.system(size:30, design: .monospaced))
Text("Rounded font design").font(Font.system(size:30, design: .rounded))
Text("Serif font design").font(Font.system(size:30, design: .serif))

Text("Large Title").font(.largeTitle)
Text("Title").font(.title)
Text("Headline").font(.headline)
Text("SubHeadline").font(.subheadline)
Text("Body").font(.body)
Text("Callout").font(.callout)
Text("Caption").font(.caption)
Text("FootNote").font(.footnote)

Text("Custom font").font(Font.custom("OpenSans-Bold", size: 12.3))

more detail Please follow: https://github.com/arjun011/SwiftUI-Controls

Solution 4 - Swift

If you want to set a custom font for your text let's say Helvetica Neue then in that case you can do something like this

Text("Hello World")
.font(.custom("Helvetica Neue", size: 20))

If you just want to go with system font then in that case you can do something like this

Text("Hello World")
.font(.system(size: 20, weight: .light, design: .default))

Solution 5 - Swift

// Using system font "San Francisco"
let fontSystem = Font.system(size: 13.0)

// Using installed custom font
let fontCustom = Font.custom("YOUR FONT NAME", size: 13.0)

You can place the font* constant to where you need to set the font style.

Read more from "Getting System Fonts" section in https://developer.apple.com/documentation/swiftui/font

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionLinusGeffarthView Question on Stackoverflow
Solution 1 - SwiftNRitHView Answer on Stackoverflow
Solution 2 - SwiftAnjali KevadiyaView Answer on Stackoverflow
Solution 3 - SwiftArjun PatelView Answer on Stackoverflow
Solution 4 - SwiftNSDumbView Answer on Stackoverflow
Solution 5 - SwiftX.CreatesView Answer on Stackoverflow