Using custom OTF fonts in Windows 8 XAML

October 31, 2012 by Håkan Reis in .Net, Tips & Tricks, User Experience | 8 Comments
spacer

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:

1
FontFamily="/[path to font file]/[font file]#[font name]"

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:

1
FontFamily="/Assets/Fonts/Basic-Regular.otf#Basic"

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:

1
FontFamily="/Assets/Fonts/Basic-ExtraBold.otf#Basic ExtraBold"

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!

spacer Getting 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.

Fonts OTF Win8 windows 8 windows store xaml

8 Comments

  1. Thomas
    December 10, 2012 at 22:27 / Reply

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

  2. Bob Vork
    December 12, 2012 at 10:51 / Reply

    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?

    • Håkan Reis
      December 12, 2012 at 14:19 / Reply

      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
    May 4, 2013 at 10:35 / Reply

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

  4. Angel
    September 4, 2013 at 15:24 / Reply

    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?

    • Håkan Reis
      September 6, 2013 at 12:27 / Reply

      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.

Trackbacks for this post

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

Leave a Reply / Cancel Reply

gipoco.com is neither affiliated with the authors of this page nor responsible for its contents. This is a safe-cache copy of the original web site.