Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WatchKit WKInterfaceLabel can't change font

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.

like image 405
Stefo Avatar asked Jan 26 '15 09:01

Stefo


1 Answers

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 Fonts provided by application (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.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!

    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.

like image 74
Ben Morrow Avatar answered Nov 13 '22 07:11

Ben Morrow