Navya
Navya

Reputation: 39

dynamic font size, style as per the resolution of screen

I am trying to give the font size, style as per typography & weight as per the resolution of screen dynamically.

I tried:

title_home.font = UIFont.preferredFont(forTextStyle: UIFont.TextStyle.title1)
 subtitle_home.font = UIFont.preferredFont(forTextStyle: UIFont.TextStyle.title2)
 subtitle1_home.font = UIFont.preferredFont(forTextStyle: UIFont.TextStyle.title2)
subtitle2_home.font = UIFont.preferredFont(forTextStyle: UIFont.TextStyle.title2)

But am getting the text in normal regular size. I need the title as bold with some size, and it should change as per the resolution of screen.

Upvotes: 0

Views: 547

Answers (1)

Mindaugas
Mindaugas

Reputation: 1735

You can get bold/italic/etc preferred font based on descriptor, something like this:

    let descriptor = UIFontDescriptor.preferredFontDescriptor(withTextStyle: .headline)
        .addingAttributes([.traits : [UIFontDescriptor.TraitKey.weight: UIFont.Weight.semibold]])

    let semiboldFont = UIFont(descriptor: descriptor, size: 0)

    print(semiboldFont)
    //<UICTFont: 0x7fe03300cc30> font-family: ".SFUIText-Semibold"; font-weight: bold; font-style: normal; font-size: 17.00pt

hope that helps

Upvotes: 1

Related Questions