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.


(原文: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.)


(原文: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.)


(原文:Click [here to see the answer][1] [1]:…)


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:")
  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])
    fontSize = CGFloat(36)
    text = "right on!"
    cstmFont = UIFont(name: "Exo-Regular", size: fontSize)!
    attrStr = NSAttributedString(string: text, attributes: 
      [NSFontAttributeName: cstmFont])
  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 edited the answer with pictures. Let me know how it goes!)