Silverlight Tip of the Day #46 – Font Support in Silverlight
Default Fonts
By Default, Silverlight supports the following local fonts (as shown rendered in Silverlight):
You can also choose from a long list of East Asian fonts if they are available on your local computer:
- Batang
- BatangChe
- DFKai-SB
- Dotum
- DutumChe
- FangSong
- GulimChe
- Gungsuh
- GungsuhChe
- KaiTi
- Malgun Gothic
- Meiryo
- Microsoft JhengHei
- Microsoft YaHei
- MingLiU
- MingLiu_HKSCS
- MingLiu_HKSCS-ExtB
- MingLiu-ExtB
- MS Gothic
- MS Mincho
- MS PGothic
- MS PMincho
- MS UI Gothic
- NSimSun
- NSimSun-18030
- PMingLiU
- PMingLiu-ExtB
- SimHei
- SimSun
- SimSun-18030
- SimSun-ExtB
Custom Fonts
In addition to the default fonts users can use their own custom made or purchased fonts. Make certain you have the legal rights to using these fonts in your application.
The steps you need to take to import and use a custom font are:
- Right click on your Silverlight application project folder in the Solution Explorer, choose “Add->New Item…”
- Browse to your font, select it and click the OK button.
- Select the font, in the property grid set the Build Action = “Resource” and the “Copy to Output Directory” = “Copy if newer”:
- Now, in your XAML, in order to reference the font in an element like a TextBlock use the following syntax in the attribute FontFamily as highlighted in yellow below - FontFamily=”[FontFileName]#[FontFriendlyName]”
Example: <TextBlock Text="Hi" FontFamily="Century.ttf#Century"></TextBlock>
- To get the exact name of the font just double click on the font in the solution explorer to open the font viewer:
Supported Types.
The following types of user added fonts are supported: TTF, ODTTF and OTF. Silverlight will not recognize other font types such as FON and TTC that are added by a user. If, however, the system has these fonts already installed they will be recognized.
Additional Notes
You may have noticed from Tip of the Day #45 that Silverlight provides an option for text called FontStretch that allows you to stretch the font. However, when applied to normal fonts like “Arial”, “Verdana”, “Times New Roman”, etc you might notice that nothing happens. This is because Silverlight does not do any stretch emulation. This option must be applied to fonts that specifically support it such as Adobe Open type (OTF) fonts.
Thank you,
--Mike Snow
Subscribe in a reader