Skip to content

Sheets losing their custom fonts when you drag them #3

@jgale

Description

@jgale

First, thanks for a brilliant package. This makes dealing with custom fonts in SwiftUI so nice. :)

I'm encountering a really weird bug. When I used Text("string").scaledFont() in a .sheet(), everything works fine once it's presented. All the custom scaled fonts are available. When I drag the sheet, the fonts lose all formatting. See this gif:

disappearing_font

The code for this is as simple as you'd expect. I'd share the whole package but can't share the fonts.

struct ContentView: View {
    @State private var sheetDisplayed = false

    var body: some View {
        Button("Show Sheet") {
            sheetDisplayed = true
        }
        .padding()
        .sheet(isPresented: $sheetDisplayed, content: {
            HowItWorks()
        })
    }
}

struct HowItWorks: View {
    @Environment(\.presentationMode) private var presentationMode

    var body: some View {
        VStack(spacing: 30) {
            Text("How it works")
                // This will not cause bug:
                // .scaledCustomFont(name: "GothamNarrSSm-Bold", size: 24)
                // This causes bug:
                .scaledFont(.headline)
                //.font(.largeTitle)
                .padding(.bottom, 30)

            Text("Last step")
                .scaledFont(.subheadline)
            Text("This is the last step. We will finish the process and then it's all done. Hooray!")
                .scaledFont(.callout)

            Button("Got it", action: { presentationMode.wrappedValue.dismiss() })
        }
        .padding()
    }
}

How ScaledFont is initialized:

@main
struct ScaledFontSheetBugApp: App {
    var body: some Scene {
        WindowGroup {
            ContentView()
                .environment(\.scaledFont, ScaledFont(fontName: "Gotham"))
        }
    }
}

When I use the commented out .scaledCustomFont line, which is what I used before discovering your package, it works fine. Here's the definition of that:

struct ScaledCustomFont: ViewModifier {
    @Environment(\.sizeCategory) var sizeCategory
    var name: String
    var size: CGFloat

    func body(content: Content) -> some View {
       let scaledSize = UIFontMetrics.default.scaledValue(for: size)
        return content.font(.custom(name, size: scaledSize))
    }
}

extension View {
    func scaledCustomFont(name: String, size: CGFloat) -> some View {
        return modifier(ScaledCustomFont(name: name, size: size))
    }
}

Any idea what might be causing SwiftUI to lose all the fonts?

Metadata

Metadata

Assignees

No one assigned

    Labels

    documentationImprovements or additions to documentation

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions