#814 – You Can Specify a List of Fonts to Try

You normally indicate the font or typeface to use in rendering a control by specifying a value for the FontFamily property.

        <TextBlock Padding="20,10" FontSize="16"
                   FontFamily="Corbel">
            I drank what?  --Socrates
        </TextBlock>

814-001
However, you can also supply a list of fonts for the FontFamily property.  WPF will attempt to use the first font listed, but if the font is not installed on the target system, it will fall back to the next font in the list.  This fallback mechanism will continue, as WPF tries each font in the list.  If none of the fonts listed are found, WPF will use the default font, Segoe UI.

        <TextBlock Padding="20,10" FontSize="16"
                   FontFamily="Baskerville,Georgia">
            I drank what?  --Socrates
        </TextBlock>

814-002

Advertisement

#336 – Intellisense Shows Fonts Available on Development System

When you use the XAML editor in Visual Studio 2010 to enter a value for the FontFamily property, Intellisense in the editor will show you a list of fonts available on the development system.  In the example below, the dropdown shows the Parsons font as a possible font choice, since it has been installed on the development system.

This font will appear correctly when the application is run on the development system, but will get displayed on a target system only if it is also installed on that system.  If the font is not present, some other font will be substituted.

#335 – How WPF Finds Fonts on a Target System

A WPF application specifies a font that it wants using the properties FontFamily, FontStyle, FontWeight, FontStretch and FontSize.  At runtime, WPF decides the exact font to use on the target system where the application is running. This decision is based on matching the first four properties (ignoring FontSize for the moment) to a physical font file that is installed on that system.

WPF starts by matching the supplied FontFamily against the names of the fonts found on the system.  It then tries to find a font that most closely matches the requested FontStretch, FontStyle and FontWeight property values.  Matching FontStretch is the highest priority, followed by FontStyle and then FontWeight.

If WPF can’t find a matching font, it “falls back” to a default font installed with WPF, C:\Windows\Fonts\GlobalUserInterface.CompositeFont.  This is a composite font that tries to map individual characters to fonts likely to be present on the system.

#334 – Specifying Values for FontFamily

The FontFamily property of a Control indicates what typeface the control’s text should be rendered in.  The value of the property can be the name of any of the fonts currently installed on the system where a WPF application is running.  In Windows 7, there are a number of fonts installed by default that can be assumed to always be present.

        <TextBlock Margin="20,20,20,0" FontSize="18" FontFamily="Candara" TextWrapping="Wrap">
            Freedom is never voluntarily given by the oppressor; it must be demanded by the oppressed.<LineBreak/>
            --Martin Luther King, Jr.
        </TextBlock>

        <TextBlock Margin="20,40,20,0" FontSize="18" FontFamily="Constantia" TextWrapping="Wrap">
            Freedom is never dear at any price. It is the breath of life. What would a man not pay for living?<LineBreak/>
            --Mohandas Gandhi
        </TextBlock>

#268 – Default FontFamily and FontSize

In WPF, the default font family for text displayed on controls (like Label and Button) is Segoe UI, with a default size of 12.0 device-independent units.

Because a device-independent unit is 1/96 inch, a FontSize value of 12 represents characters whose capitals are 1/8″ high.  This is also equivalent to a size of 9 points.

Windows also uses 9 point Segoe UI as its default system font (in Windows 7).

Most books use a font that is 11 or 12 pts (14-16 WPF units).  Paperbacks often have slightly smaller type, e.g. 10 pt (13-1/3 WPF Units).

Web sites will also typically use fonts for body text that results in text that ranges from 10-12 points.

Microsoft Word uses 11 pt as its default font size.

 

 

#263 – Specifying Font Properties

In WPF, you specify the font for text elements by specifying values for one or more font-related properties.

There are five main properties for specifying font characteristics, found in the Control class.

  • FontFamily – The typeface, e.g. Arial
  • FontSize – The size of the text, in device independent units, points, inches, or centimeters
  • FontStretch – Optionally stretch/compress the font
  • FontStyle – Set font as italic (e.g. Italic, Oblique)
  • FontWeight – Set font as bold or light (e.g. Bold, Light, Extra-Bold, etc)

Below is a simple example.

	<TextBlock FontFamily="Cambria" FontSize="14 pt" FontStyle="Normal" FontWeight="Normal"
		HorizontalAlignment="Center" VerticalAlignment="Center" Width="300" Height="100" TextWrapping="Wrap">
		A man who carries a cat by the tail learns something he can learn in no other way.
	</TextBlock>