D'Arcy from Winnipeg
Solution Architecture, Business & Entrepreneurship, Microsoft, and Adoption

Silverlight: Using Non-Standard Fonts

Sunday, November 16, 2008 8:54 PM


I had a custom style defined in a Silverlight application…

image

I wanted text using this style to display using the Impact font, which is common to Microsoft Windows font sets. When I ran my app however, it never displayed the text with Impact font. I wasn’t getting any information about why, so I thought I’d try it in Blend. Low and behold, Blend gives me a helpful message…

image

Embed it you say? After doing some online research I discovered the answer.

Silverlight Default Fonts

Silverlight out of the box comes with only a handful of fonts that you can use in your applications: Arial, Arial Black, Comic Sans MS, Courier New, Lucida Grande, Lucida Sans Unicode, Times New Roman, Trebuchet MS, Verdana, and a default called Portable Interface Font.

But that’s it. So what if you have a different font?

Embedding Fonts

You need to embed your font as a resource to your project. It goes like this:

1. Add the .ttf file to your project.
2. Go to the properties of the font file, and set its build action property to “Resource”

Voila! When you compile the project, you’ll now embed the file into your project and make the font available. However, its not *as* easy as slapping in Font Family = “Impact” all over the place.

Using Embedded Fonts In XAML

There’s a special syntax that you have to use if you want to assign the font to an element.

FileName.ttf#NameOfFont

So for instance…

Impact.ttf#Impact

You can’t just say Impact…you need to specify the file name that’s been embedded as well as a pound sign (#) in between the file name and the font name. Remember that the font name can be different from the file name.

More Resources

This is a summary of what I learned from a great video by Tim Heuer, which you can find at this link to a blog post he did showing the old way of embedding fonts.

D




Feedback

No comments posted yet.


Post a comment