动态字体大小,按照屏幕分辨率的样式

问题描述 投票:0回答:1

我试图根据屏幕的分辨率动态地给出字体大小,按照排版和重量的样式。

我试过了:

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)

但我正在以正常的常规尺寸获取文本。我需要一些大小的标题,它应该根据屏幕的分辨率改变。

ios swift uifont
1个回答
0
投票

您可以根据描述符获得粗体/斜体/等首选字体,如下所示:

    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

希望有所帮助

© www.soinside.com 2019 - 2024. All rights reserved.