Using custom OTF fonts in Windows 8 XAML

Maybe it should be part of my Styling Windows 8 series, but this was a problem that has haunted me for a couple of days. Now, that I finally solved it I might as well share it to get it out of my system.

Problem

We are building a prototype for one of our clients and they are using a custom set of fonts. The fonts comes as separate files for each variant; medium, light, regular, extra bold, italic, etc. The font is an Open Type Font (OTF) and it’s supported by Windows store apps. I started out as usual, adding the fonts to my assets. Then I tried to set it on the text style templates that I used. But no matter what I did, I was unable to get it to work. Not in design time, not in runtime. I was about to give up but I thought I should have one last go. So I open up a blank solution, added a text for each of the variants and started a strict systematic run. I tried  lot’s of setups, added the fonts to the root and in subfolders, etc.

What I did

For each variant I also added a second control text with normal Segoe UI, just to make sure I got the right font, (the font was not that far off from Segoe, so I started to go font blind). When all variants were in place I ran the app. Wow, there in the middle, I had one text that actually did work at runtime. That was the first sign that I was doing something right. So I started to make sure all other texts used the exact same format. The format for defining custom fonts are:

The clue was in the #[font name] part, the definition that did work had the root name of the font here. As an example say the font is named Basic, so the definition lookes like this:

If I just double click on the font file it will open up with the built in viewer. If you look at the top you will see Font name: [font name]. Again, with the example Basic font and the ExtraBold variant, this name now read Name: Basic ExtraBold. That sure seems correct and this is what Blend uses to automatically name the FontFamily, like this:

However, it in this case the font name should still be the root name: Basic, going over and renaming all the #[font name] of the variants resetting it to #Basic, it started to work as expected, at least in runtime. Sweet success!

fontsGetting it to show in Blend

After the first solution was in place I was almost satisfied, I could live with just runtime and not design time. But on the way home it hit me, it was such an obvious solution that people on the bus looked as I banged my hand to the forehead. If you haven’t figured it out yet, the solution was simply to install the font to my system…duh! Just make sure you have the font’s as assets as well or you can end up with a solution that just work on your computer.

So in summary, make sure the #[font name] is correct and install the font so it’s visible at design time in Blend…much better now.

12 Comments

  1. Thomas

    Thank you so much! Saved me a lot of time!!

  2. Hi, thanks for the article, but I still can’t get this to work. Any .ttf font I try works, but none of the .otf fonts will. I think I’ve tried all possible variations of #[font name] at least 4 times, but still nothing. Is there anything else that needs to be done for otf fonts to work?

    • Nope, I haven’t found that there is something more to be done to get it to work. But I have found that some OTF fonts do not seem to be generated correctly. I haven’t studied that in detail but it seem to be that blend are very sensitive to errors or mismatch metadata in the OTF file.

      If you want we can take the discussion off-line and do some testing.

  3. Alberto

    Thanks! I forgot to install the font in my system, a silly absent-mindedness always takes a lot time

  4. Angel

    I have tried this but cant get it to work for any .otf font that I need for the application. Also I’m having problems with some .ttf files. I added the file to my project and also I installed the font on my PC. They work fine on text editors(Word, WordPad) but they still dont display in my app.

    Any sugestions?

    • Make sure you try it out in a very small sample app first to isolate all other issues. Doing that, whenever i didn’t get it to work it was one of two things:

      – The path to the resource where the font file is placed (/Assets/Fonts/Basic-Regular.otf in this case) make sure it’s added correctly in the project
      – but mostly it was the second part that caused problems. You have to experiment to get this right as I haven’t found a systematic solution to this.

      We can take it via mail if you want to examine it a bit more.

  5. Matt Becker

    Thanks for the article! I had used another custom made font that worked with just /[path to font file]/#[font name], so I was confused when another font that I downloaded didn’t work the same.

    This really helped.

  6. Matt Becker

    I tested further and with my particular font Proxima Nova, I was able to leave the font weight of in the name out.
    For example: FontFamily=”/Resources/ProximaNova-Thin.otf#Proxima Nova” Worked since the file specified was Thin already. I guess it all depends upon the particular font you’re using.

  7. Håkan Reis

    Yes, I wasn’t able to find a particular pattern, as you say – It seems to depend on the font used.

  8. Dmitry

    Adding prefix “ms-appx” help for me.
    FontFamily=”ms-appx:/Assets/Fonts/dinpro-light.ttf#DINPro”

Trackbacks for this post

  1. Windows 8 Developer Links – 2012-11-02 | Dan Rigby
  2. Fonts as scalable vector graphics – Jayway

Leave a Reply