ios - 如何在 Swift 4/Xcode 10.1 中的整个应用程序中使用自定义字体?

标签 ios swift uilabel

<分区>

我在这里尝试了一些答案来为整个应用程序设置自定义字体,但似乎没有一个在 Xcode 10.1 中工作。我想知道是否有人有一些建议?

这是我到目前为止尝试过的;
Using custom font for entire iOS app swift

Set a default font for whole iOS app?

这是一个代码示例:(这仍然给我系统字体)

override func viewDidLoad() {
    super.viewDidLoad()
    UILabel.appearance().font = UIFont(name: "Times New Roman", size: 17)

    let label1 = UILabel(frame: CGRect(x: 50, y: 50, width: 100, height: 100))
    label1.text = "Hello"
    view.addSubview(label1)
}

谢谢!

最佳答案

你可以使用 UIFont 的扩展

extension UIFont {

   public enum FontWeight {
      //cases are just font family like bold, light, italic
      case .light
   }


   func appFont(weight : FontWeight, size : CGFloat) -> UIFont {
      var fontName = "Times New Roman-"
      switch weight {
         case .light {
             fontName = fontName + "light" // add your family name here
         }
      }
      return UIFont(name: fontName, size: size)!
    }
}

如果是标签

label.font = UIFont.appFont(weight : .light, size : 17)

关于ios - 如何在 Swift 4/Xcode 10.1 中的整个应用程序中使用自定义字体?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54643860/

相关文章:

objective-c - 取消dispatch_io_read

ios - UIButton 上的 UILabel 突变导致文本不显示/显示对齐错误

json - 使用 Facebook iOS SDK 时 PhoneGap 和 SBJSON 重复冲突

ios - 使用 Swift 对日语字符进行排序

ios - If 带日期的语句

ios - AWS Swift 入门(主要问题)

ios - 无法在 spritekit [SWIFT] 中为 GameScene 设置正确的大小

swift - 在 Swift 3 错误中访问代码

iphone - UIFont 中符号的使用

ios - 使用 UILabel 文本掩码显示 UIView