UIFont - 如何获得系统瘦字体

Mig*_*Slv 95 objective-c uikit uifont ios swift

UIFont有获取常规font(systemFontOfSize)或粗体字体(boldSystemFontOfSize)的方法,但如何通过故事板获得"瘦系统字体"?

将"system-thin"传递给UIFontContructor不起作用,此构造函数仅适用于非系统字体.

s1d*_*dok 258

您可以使用系统字体瘦重:

UIFont.systemFont(ofSize: 34, weight: UIFontWeightThin)
Run Code Online (Sandbox Code Playgroud)

旧金山的可用重量列表:

UIFontWeightUltraLight
UIFontWeightThin
UIFontWeightLight
UIFontWeightRegular
UIFontWeightMedium
UIFontWeightSemibold
UIFontWeightBold
UIFontWeightHeavy
UIFontWeightBlack
Run Code Online (Sandbox Code Playgroud)

旧金山字体重量

从iOS 11开始, UIFontWeight*重命名为UIFont.Weight.*.您可以访问https://developer.apple.com/documentation/uikit/uifont.weight.

  • 仅适用于**iOS 8.2**及更高版本,thnx (10认同)

Fah*_*kar 31

从iOS 8.2开始,您现在可以使用UIFont.systemFontOfSize(_ fontSize: CGFloat, weight weight: CGFloat):

UIFont.systemFontOfSize(19, weight: UIFontWeightLight)

iOS SDK为权重提供了常量:

UIFontWeightUltraLight
UIFontWeightThin
UIFontWeightLight
UIFontWeightRegular
UIFontWeightMedium
UIFontWeightSemibold
UIFontWeightBold
UIFontWeightHeavy
Run Code Online (Sandbox Code Playgroud)

当你想使用系统字体时,使用系统字体比创建基于字体名称的字体更好,因为iOS可以在iOS上更改他们的系统字体(就像他们在iOS 7中使用Helvetica Neue,现在在iOS 9中使用旧金山) .

所以我建议包括你想要使用ttf文件作为自定义字体的字体的TTF文件,并在你的应用程序中使用自定义字体.

这就是我不喜欢Apple的特殊原因. 永远不要像 Apple说的那样.永远做我们想做的事. Apple继续更改 每个操作系统的默认字体.


Gra*_*lix 7

此外,如果您想保持相同的字体大小并只是改变重量,那么请使用目标元素字体大小.例如:

demoLabel.font = UIFont.systemFont(ofSize: demoLabel.font.pointSize, weight: UIFontWeightThin)
Run Code Online (Sandbox Code Playgroud)

使用此功能,您可以保留默认标签字体大小,只需更改重量.

从iOS 11开始, UIFontWeightThin被重命名为UIFont.Weight.thin.您可以访问https://developer.apple.com/documentation/uikit/uifont.weight.