WatchKit WKInterfaceLabel不能更改字体 [英] WatchKit WKInterfaceLabel can't change font

查看:198
本文介绍了WatchKit WKInterfaceLabel不能更改字体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图改变标签字体。但是我在属性检查器中设置的字体与系统字体不同,不要更改任何内容 - 既不是模拟器也不是故事板。我甚至尝试用Attributed字符串编程设置字体 - 出现相同的系统字体。谢谢你的帮助。

解决方案

目前您无法在WatchKit中使用随附的iOS字体。唯一可用的是系统(旧金山)。


  • 包含自定义字体同时在WatchKit应用程序和WatchKit
    扩展程序包中生成文件。




  • 将应用程序(UIAppFonts)键提供的字体添加到 WatchKit应用程序 WatchKit Extension Info.plist 文件


  • 将此代码添加到 awakeWithContext 知道正确的字体名称,以便在以后的代码中调用:

    pre $ print $(自定义字体名称)
    print (UIFont.fontNames(forFamilyName:Exo))
    print(UIFont.fontNames(forFamilyName:Tabardo))


  • 运行应用程序并记下打印到调试控制台的字体名称。一旦你知道了正确的名字,你可以添加这个代码在你的WatchKit扩展的某个地方:
    $ b $ pre $ code 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)
    $ b $ fontSize = CGFloat(36)
    text =right on!
    cstmFont = UIFont(名称:Exo-Regular,size:fontSize)!
    attrStr = NSAttributedString(string:text,attributes:
    [NSFontAttributeName:cstmFont])
    secondLabel.setAttributedText(attrStr)

  • 享受手表上的自定义字体! 使用自定义字体的手表界面>


  • 请注意,浏览和通知无法使用自定义字体。如果你想在那里使用一个,你将不得不使用渲染的图像。但是,因为浏览和通知应该加载得很快,所以你希望在被调用的时候准备好映像。


    I'm trying to change label font. But every font I set in the Attributes Inspector different from the system font - don't change anything - neither simulator or storyboard. I even tried to set the font programatically with Attributed string - the same System font appears. Thanks for your help.

    解决方案

    You cannot use the included iOS fonts in WatchKit at this time. The only one available is System (San Francisco). Source: Apple Developer Forums

    You can however use a custom font by adding the font file to the project:

    1. Drag the font files into the project navigator

    2. Include the custom font file in both your WatchKit app and WatchKit extension bundle.

    3. Add the Fonts provided by application (UIAppFonts) key to both your WatchKit app and your WatchKit Extension Info.plist files

    4. Add this code to awakeWithContext to make sure you know the correct font name to call later in your code:

      print("Custom font names:")
      print(UIFont.fontNames(forFamilyName: "Exo"))
      print(UIFont.fontNames(forFamilyName: "Tabardo")) 
      

    5. Run the app and take note of the font names printed to the debug console. Once you know the correct name, you can add this code somewhere in your WatchKit Extension:

      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. Enjoy the custom fonts on the watch!

    Keep in mind that glances and notifications cannot use custom fonts. If you want to use one there, you'll have to use a rendered image. However because glances and notifications should load quickly you'd want to have that image ready to go when it gets called.

    这篇关于WatchKit WKInterfaceLabel不能更改字体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆