简体   繁体   中英

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM