gpt4 book ai didi

ios - WatchKit WKInterfaceLabel 无法更改字体

转载 作者:可可西里 更新时间:2023-11-01 03:28:03 32 4
gpt4 key购买 nike

我正在尝试更改标签字体。但是我在属性检查器中设置的每种字体都与系统字体不同——不要改变任何东西——既不是模拟器也不是 Storyboard。我什至尝试使用属性字符串以编程方式设置字体 - 出现相同的系统字体。谢谢你的帮助。

最佳答案

此时您不能在 WatchKit 中使用包含的 iOS 字体。唯一可用的是系统(旧金山)。 Source: Apple Developer Forums

不过,您可以通过将字体文件添加到项目中来使用自定义字体:

  1. 将字体文件拖到项目导航器中

    font in project navigator

  2. 在您的 WatchKit 应用和 WatchKit 中包含自定义字体文件扩展包。

    add both targets

  3. 应用程序提供的字体 (UIAppFonts) 键添加到 您的 WatchKit 应用 和您的 WatchKit 扩展 Info.plist 文件

    font key in plist

  4. 将此代码添加到 awakeWithContext 以确保您知道稍后在代码中调用的正确字体名称:

    print("Custom font names:")
    print(UIFont.fontNames(forFamilyName: "Exo"))
    print(UIFont.fontNames(forFamilyName: "Tabardo"))
  5. 运行应用程序并记下打印到调试控制台的字体名称。知道正确的名称后,您可以在 WatchKit 扩展中的某处添加此代码:

    var fontSize = CGFloat(32)
    var text = "so cool"
    var cstmFont = UIFont(name: "Tabardo", size: fontSize)!
    var attrStr = NSAttributedString(string: text, attributes:
    [NSFontAttributeName: cstmFont])
    firstLabel.setAttributedText(attrStr)

    fontSize = CGFloat(36)
    text = "right on!"
    cstmFont = UIFont(name: "Exo-Regular", size: fontSize)!
    attrStr = NSAttributedString(string: text, attributes:
    [NSFontAttributeName: cstmFont])
    secondLabel.setAttributedText(attrStr)
  6. 在 watch 上享受自定义字体!

    watch interface with custom fonts

请记住,扫视和通知不能使用自定义字体。如果您想在那里使用一个,则必须使用渲染图像。但是,因为浏览和通知应该快速加载,所以您希望在调用时准备好该图像。

关于ios - WatchKit WKInterfaceLabel 无法更改字体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28147102/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com