我试图根据屏幕的分辨率动态地给出字体大小,按照排版和重量的样式。
我试过了:
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)
但我正在以正常的常规尺寸获取文本。我需要一些大小的标题,它应该根据屏幕的分辨率改变。
您可以根据描述符获得粗体/斜体/等首选字体,如下所示:
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
希望有所帮助