ios - Swift 以编程方式更改字体

标签 ios swift

您好,我目前正在设置以下 View :

func setupViews() {

    self.numberLabel = UILabel(frame: .zero) 
    self.addSubview(self.numberLabel)
    self.numberLabel.snp.makeConstraints { (make) in
        make.edges.equalToSuperview()
    }


    self.numberLabel.textAlignment = .center
    self.numberLabel.textColor = UIColor.white
    self.numberLabel.adjustsFontSizeToFitWidth = true
    self.numberLabel.minimumScaleFactor = 0.5

}

我想将标签内文本的字体更改为粗体,但是很难找到一种简单的方法来遵循上述语法原则。

最佳答案

对于 Swift 5+ 和最新版本 (Swift 5.4) 期间

Stylizing the Font (SystemFont)

UIFont.systemFont(ofSize: 17, weight: .medium)

您可以设置 .weight 的地方提供了各种 UIFont.Weight 属性,如下所示

  • .black
  • .bold
  • .heavy
  • .light
  • .medium
  • .regular
  • .semibold
  • .thin(看起来很酷,我猜 Apple 也在某处使用它)
  • .ultralight

请注意,它仅适用于默认的 SystemFontAppleDocumentation

Changing the Font

UIFont(name: "HelveticaNeue-Thin", size: 16.0)
  • 名称包括FontName
  • 您还可以通过在权重 之前写入 - 来指定 .weight(如果该字体支持该权重,因为并非所有 fontFamily 都支持全部 UIFont.Weight)
  • 的类型
  • 在以下情况下,这是更适合用于字体样式化的方法 你没有使用默认的 SystemFont

关于ios - Swift 以编程方式更改字体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46403444/

相关文章:

ios - 如何随着时间的推移永久保存文本行

ios - 将 PDF 文件附加到电子邮件 - Swift

iphone - AudioFileSetProperty 返回 'kAudioFileUnsupportedPropertyError (pty?)'

swift - 如何比较两个 UIDynamicProviderColor?

ios - 从我的应用程序获取坐标到 Uber(ios、swift4)

ios - 自 iOS 13 起,Master Detail Controller 始终以模态方式呈现

ios - 自动完成算法

iPhone/iOS : Subclassing UITableViewCell two times --> cannot reference properties of parentclass in InterfaceBuilder

ios - 无法理解崩溃的堆栈跟踪

ios - 无法在 Swift 的 ViewController 类中创建子类的对象