WatchKitWKInterfaceLabel不能改变字体 - WatchKit WKInterfaceLabel can't change font

- 此内容更新于:2015-12-13
主题:

我试图改变标签的字体。但每个字体我在属性检查器中设置不同的系统字体,不要改变什么——无论是模拟器或故事板。我甚至尝试设置字体编程上带属性字符串——相同的系统字体出现。谢谢你的帮助。

原文:

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.

网友:你用最近的β(Xcode6.2beta4)?似乎打破了自上次更新。我不改变字体实验程序,但可能有更多的东西坏了。

(原文:Are you using the most recent beta (Xcode 6.2 beta 4)? WKInterfaceLabel -setHidden: seems to be broken since the last update. I don't change font in my experimental app, but there may be more stuff broken in WKInterfaceLabel.)

楼主:是的,我最后Xcode6.2beta4。我希望有解决办法或所有我能做的就是等到下一个β5。

(原文:Yes, I'm with the last Xcode 6.2 beta 4. I hope to have solution or all I can do is to wait till next beta 5.)

网友:单击[这里查看答案][1][1]:stackoverflow.com/questions/28360525/?

(原文:Click [here to see the answer][1] [1]: stackoverflow.com/questions/28360525/…)

解决方案:
你不能使用包括iOS字体WatchKit。只有一个可用的系统(旧金山)。来源:苹果开发者论坛你可以使用自定义字体,字体文件添加到项目:拖动字体文件到项目导航器包括自定义字体文件WatchKit应用和WatchKit扩展包。添加UIAppFontsWatchKit应用和WatchKit扩展的关键信息。plist文件添加此代码,以确保你知道正确的字体的名字叫后来在代码中:一旦你知道正确的名字,您可以添加代码:享受定制的字体看!记住,目光和通知不能使用自定义字体。如果你想使用一个在那里,你将不得不使用一个呈现的形象。然而因为目光和通知负载应该很快你就会想要图片准备好了的时候。
原文:

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

    font in project navigator

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

    add both targets

  3. Add the UIAppFonts key to both your WatchKit app and your WatchKit Extension Info.plist files

    font key in plist

  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.fontNamesForFamilyName("Exo"))
    print(UIFont.fontNamesForFamilyName("Tabardo")) 
    
  5. Once you know the correct name, you can add your 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)
    
    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!

    watch interface with custom fonts

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.

网友:这样做,它还是不工作…

(原文:did this, and it is still not working...)

网友:@Underdog与图片编辑答案。让我知道它!

(原文:@Underdog edited the answer with pictures. Let me know how it goes!)