我希望将标签文本设置为“System Thin”。在StackOverflow上阅读并找到了以下答案:
但它没有起作用。如何改进我的代码并使细字体样式自动化?
labelDescriptionView.font = UIFont(name: "System-Thin", size: 15.0)
但它没有起作用。如何改进我的代码并使细字体样式自动化?
labelDescriptionView.font = UIFont(name: "System-Thin", size: 15.0)
在Interface Builder中的system
字体是操作系统的默认字体,你不能通过它的名称获得该字体。苹果为系统字体提供了以下方法:
+ (UIFont *)systemFontOfSize:(CGFloat)fontSize;
+ (UIFont *)boldSystemFontOfSize:(CGFloat)fontSize;
+ (UIFont *)italicSystemFontOfSize:(CGFloat)fontSize;
这些不包括任何轻量级版本,但在iOS 8.2及以上版本中,您可以使用:
+ (UIFont *)systemFontOfSize:(CGFloat)fontSize weight:(CGFloat)weight;
你可以通过以下方式:作为权重:
UIFontWeightUltraLight
UIFontWeightThin
UIFontWeightLight
UIFontWeightRegular
UIFontWeightMedium
UIFontWeightSemibold
UIFontWeightBold
UIFontWeightHeavy
所以一个细的系统字体会是:
UIFont *thinFont = [UIFont systemFontOfSize:15 weight:UIFontWeightThin];
如果你使用的是 iOS 8.2 或更高版本,则可以使用此功能;
label.font = UIFont.systemFontOfSize(15, weight: UIFontWeightThin)
对于先前版本,只需使用HelveticaNeue-Thin
作为您的字体。
编辑:对于iOS 14,它是:
label.font = UIFont.systemFont(ofSize: 14, weight: .light)