daspianist
daspianist

Reputation: 5495

Dynamic sizing fonts based on phone screen

I am interested in adjusting the size of the fonts I use dynamically based on the display device. The app is configured to only allow portrait-up display.

struct DisplayFonts {

    static let NavBarTitleFont = UIFont.init(name: "Montserrat-Bold", size: 20.0*fontSizeMultipler) //error 

    private var fontSizeMultipler: CGFloat {
        get {
            let screenSize = UIScreen.main.bounds
            let screenWidth: CGFloat = screenSize.width
            let templateScreenWidth: CGFloat = 375.0

            return screenWidth / templateScreenWidth

        }
    }
}

As noted in the comments, I get the error Cannot use instance member fontSizeMultipler. I was wondering if there is another way to go about using my current struct so the font size could be updated by the screen size? Thaks!

Upvotes: 0

Views: 41

Answers (3)

Arash Tabrizian
Arash Tabrizian

Reputation: 146

This error is completely clear. You are using "fontSizeMultipler" to calculate a value for a property that is defined "static" so you need to define "fontSizeMultipler" as static too

Upvotes: 0

Eugene Laminskiy
Eugene Laminskiy

Reputation: 704

You should declare you fontSizeMultipler variable as static also:

private static var fontSizeMultipler: CGFloat

Upvotes: 1

KonDeichmann
KonDeichmann

Reputation: 968

you just did a minor Mistake:

struct DisplayFonts {

    static let NavBarTitleFont = UIFont.init(name: "Montserrat-Bold", size: 20.0*fontSizeMultipler)

    private static var fontSizeMultipler: CGFloat {
        get {
            let screenSize = UIScreen.main.bounds
            let screenWidth: CGFloat = screenSize.width
            let templateScreenWidth: CGFloat = 375.0

            return screenWidth / templateScreenWidth

        }
    }
}

See how i changed the the var fontSizeMultipler to static, now both Variables are on the same visibility level, now it should work.

Upvotes: 1

Related Questions