动态字体大小,样式取决于屏幕分辨率

时间:2019-04-19 04:29:53

标签: ios swift uifont

我试图动态提供字体大小,样式,字体和粗细,以及屏幕分辨率。

我尝试过:

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)

但是正在以正常的常规尺寸显示文本。我需要标题为粗体,并且要有一定的大小,并且标题应该根据屏幕的分辨率进行更改。

1 个答案:

答案 0 :(得分: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

希望有帮助