【问题标题】:How to set text font as System Thin in Swift? [duplicate]如何在 Swift 中将文本字体设置为 System Thin? [复制]
【发布时间】:2016-03-04 04:04:45
【问题描述】:

我想将标签文本设置为 System Thin。阅读 StackOverflow 并找到这样的答案:

labelDescriptionView.font = UIFont(name: "System-Thin", size: 15.0)

但它不起作用。如何改进我的代码并以编程方式制作细字体样式?

【问题讨论】:

  • 你在哪里找到的?
  • @MartinR 哦,我误解了答案=/
  • @OrkhanAlizade 它已经给出,设置字体名称“HelveticaNeue-Thin”,因为 iOS 使用“HelveticaNeue”作为默认字体。 iOS 中没有指定“System-Thin”字体,只有 Bold、Italic 和 Regular 用于 System 字体。
  • @DipenPanchasara 自 iOS 9 起 HelveticaNeue 不再是默认字体!

标签: ios swift fonts programmatically-created


【解决方案1】:

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];

【讨论】:

    【解决方案2】:

    如果您使用的是 iOS 8.2 或更高版本,则可以使用它;

    label.font = UIFont.systemFontOfSize(15, weight: UIFontWeightThin)
    

    对于以前的版本,只需使用 HelveticaNeue-Thin 作为您的字体。

    编辑:对于 iOS 14,它是:

    label.font = UIFont.systemFont(ofSize: 14, weight: .light)
    

    【讨论】:

      猜你喜欢
      • 2019-10-22
      • 2014-04-01
      • 1970-01-01
      • 2011-09-10
      • 2016-08-30
      • 2015-04-09
      • 1970-01-01
      • 2015-11-02
      • 1970-01-01
      相关资源
      最近更新 更多