Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change default system font in SwiftUI

Tags:

swiftui

I'm trying to set a custom default font in my SwiftUI app. I tried several suggestions from this thread Set a default font for whole iOS app?.

However, none of those seem to work with SwiftUI. For example with this approach:

// Constants.swift
struct Fonts {
    static var plex = "IBMPlexMono-Text"
}


// FontExtension.swift
extension UILabel {
    var substituteFontName : String {
        get { return self.font.fontName }
        set { self.font = UIFont(name: Fonts.plex, size: 17)! }
    }

}

// AppDelegate.swift in didFinishLaunchingWithOptions-function
UILabel.appearance().substituteFontName = Fonts.plex

When I start the app, the custom font appears for a split second and then changes back to the default font by Apple. Why does it change back to Apple's font and how can it be done permanently?

Is it maybe possible with an extension on Text-View?

like image 382
simibac Avatar asked Nov 13 '19 17:11

simibac


People also ask

How do I change the font in SwiftUI?

Setting Text and Changing Fonts The most basic way we can add text in SwiftUI is by the use of Text() so to make it easy lets just use this as example. Text("Hello, world!") Changing Font is easy, you just need to use the dot notation called . font which accepts a Font type value.

What is the default SwiftUI font?

SwiftUI lets you customize Text by applying a . font() modifier. The default iOS font is called San Francisco and if you don't explicitly change it, then all of your text will have the default iOS look.

What is the SwiftUI default font size?

Default font size vs fixed font size of 36 points. System font has many variations for us to choose from. As you might notice, there are weight and design parameters in the Font.

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.


1 Answers

You can have:

extension Font {
    static let mediumFont = Font.custom("Sans-Regular", size: Font.TextStyle.subheadline.size, relativeTo: .caption)
    static let mediumSmallFont = Font.custom("Sans-Regular", size: Font.TextStyle.footnote.size, relativeTo: .caption)
    static let smallFont = Font.custom("Sans-Regular", size: Font.TextStyle.caption.size, relativeTo: .caption)
    static let verySmallFont = Font.custom("Sans-Regular", size: Font.TextStyle.caption2.size, relativeTo: .caption)
}

extension Font.TextStyle {
    var size: CGFloat {
        switch self {
        case .largeTitle: return 60
        case .title: return 48
        case .title2: return 34
        case .title3: return 24
        case .headline, .body: return 18
        case .subheadline, .callout: return 16
        case .footnote: return 14
        case .caption: return 12
        case .caption2: return 10
        @unknown default:
            return 8
        }
    }
}

and use it like this:

Text("Edit Profile")
   .font(.mediumSmallFont)
like image 93
Edi Avatar answered Sep 24 '22 02:09

Edi