FontFamily Class

Represents a family of related fonts.

System::Object
  System.Windows.Media::FontFamily

Namespace:  System.Windows.Media
Assembly:  PresentationCore (in PresentationCore.dll)
XMLNS for XAML: http://schemas.microsoft.com/winfx/2006/xaml/presentation, http://schemas.microsoft.com/netfx/2007/xaml/presentation

[LocalizabilityAttribute(LocalizationCategory::Font)]
[TypeConverterAttribute(typeof(FontFamilyConverter))]
public ref class FontFamily
<FontFamily .../>
<object FontFamily="fontFamilyName"/>
- or -
<object FontFamily="fontFamilyNamesList"/>
- or -
<object FontFamily="fontFamilyFolderReference"/>
- or -
<object FontFamily="fontFamilyUriReference"/>

XAML Values

fontFamilyName

A string specifying a font family name. For example, "Arial" or "Century Gothic".

fontFamilyNamesList

A string specifying multiple font family names, each separated by a comma (any whitespace following a comma is ignored). The first font family specified serves as the primary font family; subsequent font families serve as fallback families to be used in cases where the primary font family is unavailable or not applicable. For example, "Arial, Century Gothic" specifies Arial as the primary font family, with Century Gothic as the fallback font family.

fontFamilyFolderReference

A string specifying a folder containing the font, along with a font family name. The folder and font family name are delimited by a # character. The folder reference may be absolute, or relative. For example, "Custom Fonts\#My Custom Font".

fontFamilyUriReference

A string specifying a uniform resource identifier (URI) for the font, along with a font family name. The URI and font family name are delimited by a # character. For example, "http://MyFontServer/Fonts/#My Custom Font".

The FontFamily type exposes the following members.

  NameDescription
Public methodFontFamily()Initializes a new instance of an anonymous FontFamily class.
Public methodFontFamily(String)Initializes a new instance of the FontFamily class from the specified font family name.
Public methodFontFamily(Uri, String)Initializes a new instance of the FontFamily class from the specified font family name and an optional base uniform resource identifier (URI) value.
Top

  NameDescription
Public propertyBaselineGets or sets the distance between the baseline and the character cell top.
Public propertyBaseUriGets the base uniform resource identifier (URI) that is used to resolve a font family name.
Public propertyFamilyMapsGets the collection of FontFamilyMap objects.
Public propertyFamilyNamesGets a collection of strings and CultureInfo values that represent the font family names of the FontFamily object.
Public propertyFamilyTypefacesGets a collection of typefaces for the FontFamily object.
Public propertyLineSpacingGets or sets the line spacing value for the FontFamily object. The line spacing is the recommended baseline-to-baseline distance for the text in this font relative to the em size.
Public propertySourceGets the font family name that is used to construct the FontFamily object.
Top

  NameDescription
Public methodEqualsGets a value that indicates whether the current font family object and the specified font family object are the same. (Overrides Object::Equals(Object).)
Protected methodFinalizeAllows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.)
Public methodGetHashCodeServes as a hash function for FontFamily. It is suitable for use in hashing algorithms and data structures such as a hash table. (Overrides Object::GetHashCode().)
Public methodGetTypeGets the Type of the current instance. (Inherited from Object.)
Public methodGetTypefacesReturns a collection of Typeface objects that represent the type faces in the default system font location.
Protected methodMemberwiseCloneCreates a shallow copy of the current Object. (Inherited from Object.)
Public methodToStringReturns the value of the Source property. (Overrides Object::ToString().)
Top

A font family is a set of typefaces that share the same family name, such as "Times New Roman", but that differ in features. These feature differences include Style, such as italic, and Weight, such as bold.

Example of typefaces that are members of the "Times New Roman" font family

Fonts

Most user interface (UI) elements, such as Button and TextBlock, provide a FontFamily property that can be used to specify a font for the text content of a control. You define the font by setting that property with a FontFamily value. The following examples show how to reference a font, in Extensible Application Markup Language (XAML) and in code.

No code example is currently available or this language may not be supported.

<TextBlock FontFamily="Comic Sans MS">Hello, world</TextBlock>


In the preceding example, the font that is referenced, "Comic Sans MS", is referred to by its friendly name. Also, in this example the font is assumed to be in the system font collection.

Specifying Fonts in Alternate Directories

A Windows Presentation Foundation (WPF) application can specify a directory, other than the directory that contains the systems font collection, for resolving font references. The friendly name of the font can specify an absolute uniform resource identifier (URI) value to resolve the font reference, as shown in the following Extensible Application Markup Language (XAML) and code examples.

No code example is currently available or this language may not be supported.

<TextBlock FontFamily="file:///d:/MyFonts/#Pericles Light">
  Aegean Sea
</TextBlock>


The friendly name of the font can also specify a relative URI value, which requires a base URI to resolve the font reference. The BaseUri property of the FontFamily object corresponds to the base URI value. The following code example shows how to create a font reference that is composed of a base URI value and a relative URI value.

No code example is currently available or this language may not be supported.

You can use a base URI value when you reference a font that is packaged as part of the application. For example, the base URI value can be a "pack://application" URI, which lets you reference fonts that are packaged as application resources. The following code example shows a font reference that is composed of a base URI value and a relative URI value.

No code example is currently available or this language may not be supported.

When a FontFamily is specified as an attribute in markup, the base URI value is always implied—its value is the URI of the XAML page. The implied base URI value is used with the relative URI value in the friendly name string to obtain the location of the font. In the following Extensible Application Markup Language (XAML) example, notice that the relative URI value uses the "./" notation, which means "in the current folder" of the base URI value.


<TextBlock FontFamily="./resources/#Pericles Light">
  Aegean Sea
</TextBlock>


A WPF application can package fonts as either a content item, a resource item, or a library resource item. For more information, see Packaging Fonts with Applications.

Font Fallback

Font fallback refers to the automatic substitution of a font other than the font that is selected by the client application. There are two primary reasons why font fallback is invoked:

  • The font that is specified by the client application does not exist on the system.

  • The font that is specified by the client application does not contain the glyphs that are required to render text.

InWPF, the font fallback mechanism uses the default fallback font family, "Global User Interface", as the substitute font. This font is defined as a composite font, whose file name is "GlobalUserInterface.CompositeFont". For more information about composite fonts, see the Composite Fonts section in this topic.

The WPF font fallback mechanism replaces previous Win32 font substitution technologies.

Defining a Font Fallback Sequence in Code

You can define a font fallback sequence in your code, which lets you define an alternate font. When you create a FontFamily object, provide multiple font family names, separated by commas, for the String parameter, such as "Comic Sans MS, Verdana". In this case, if the glyphs from the "Comic Sans MS" typeface are not available, glyphs from the "Verdana" typeface are used. If neither "Comic Sans MS" nor "Verdana" have the required glyphs, the fallback font family of the typeface is used, which is "Global User Interface" by default.

The following examples show how to define a font fallback sequence, in Extensible Application Markup Language (XAML) and in code.

No code example is currently available or this language may not be supported.

<TextBlock FontFamily="Comic Sans MS, Verdana">Hello, world</TextBlock>


Any one of the fonts in the fallback sequence can specify font locations. In the following examples, "Pericles Light" is referenced as an application resource, and "Verdana" is referenced as a system font collection member.

No code example is currently available or this language may not be supported.

<TextBlock FontFamily="./resources/#Pericles Light, Verdana">Aegean Sea</TextBlock>


Composite Fonts

The WPF platform provides a composite font feature to allow the construction of full range multilingual fonts, and to avoid displaying missing glyphs. Composite fonts replace the Win32 font linking, font fallback, font binding, font association, and end-user-defined characters (EUDC) mechanisms.

A composite font family is available to applications through the FontFamily and Typeface constructors just like any other font family. Each composite font family is named, and, as with other fonts, can provide localized variants of its name in multiple languages.

The following markup example shows how a composite font family can be defined as a disk file. This file can be stored in the default Windows font directory as with any other installed font, or can be referenced in any location by including its URI when referencing the family by name.

The following example shows the font family markup in a ".CompositeFont" file.


<FontFamily
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/composite-font"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:System="clr-namespace:System;assembly=mscorlib"
    Baseline="0.9"
    LineSpacing="1.2">

    <!-- Name mapping -->
    <FontFamily.FamilyNames>
        <System:String x:Key="en-US">Global User Interface</System:String>
    </FontFamily.FamilyNames>

    <!-- Faces to report in font chooser UI -->
    <FontFamily.FamilyTypefaces>
        <FamilyTypeface
            Weight="Normal" Stretch="Normal" Style="Normal"
            UnderlinePosition="-0.1" UnderlineThickness="0.05"
            StrikethroughPosition="0.3" StrikethroughThickness="0.05"
            CapsHeight="0.5" XHeight="0.3" />

        <FamilyTypeface
            Weight="Bold" Stretch="Normal" Style="Normal"
            UnderlinePosition="-0.1" UnderlineThickness="0.05"
            StrikethroughPosition="0.3" StrikethroughThickness="0.05"
            CapsHeight="0.5" XHeight="0.3" />
    </FontFamily.FamilyTypefaces>

    <!-- Character to family lookups (in lookup order) -->
    <FontFamily.FamilyMaps>

        <!--
            Basic Latin                 0000-007F
            Latin-1 Supplement          0080-00FF
            Latin Extended-A            0100-017F
            Latin Extended-B            0180-024F
            IPA Extensions              0250-02AF
            Spacing Modifier Letters    02B0-02FF 
            Combining Diacritics Marks  0300-036F 
            Greek and Coptic            0370-03FF
            Cyrillic                    0400-04FF 
            Cyrillic Supplement         0500-052F 
            Phonetic Extensions         1D00-1D7F
            Latin Extended Additional   1E00-1EFF
            Greek Extended              1F00-1FFF
            Alpha Pres Forms Latin      FB00-FB0F -->
        <!-- CHS -->    
        <FontFamilyMap
            Unicode="0000-052F, 1D00-1FFF, FB00-FB0F"
            Language="zh-Hans"
            Target="Times New Roman"
            Scale="1.0" />
        <!-- CHT -->    
        <FontFamilyMap
            Unicode="0000-052F, 1D00-1FFF, FB00-FB0F"
            Language="zh-Hant"
            Target="Times New Roman"
            Scale="1.0" />
        <!-- Other (include JA and KO) -->    
        <FontFamilyMap
            Unicode="0000-052F, 1D00-1FFF, FB00-FB0F"
            Target="Comic Sans MS, Verdana"
            Scale="4.0" />

        <!--
            Armenian                    0530-058F    
            Georgian                    10D0-10FF
            Alpha Pres Forms Armenian   FB10-FB1C -->
        <FontFamilyMap
            Unicode="0530-058F, 10D0-10FF, FB10-FB1C"
            Target="Sylfaen"
            Scale="1.0" />

        <!-- Other FontFamilyMap elements defined ... --> 

    </FontFamily.FamilyMaps>

</FontFamily>


The following four composite fonts appear in the default Windows font directory as part of the WPF installation.

Font

Notes

GlobalMonospace.CompositeFont

Renders text by using a monospace font, for example, "Courier New" for Latin characters.

GlobalSanSerif.CompositeFont

Renders text by using a sans serif font, for example, "Arial" for Latin characters.

GlobalSerif.CompositeFont

Renders text by using a serif font, for example, "Times New Roman" for Latin characters.

GlobalUserInterface.CompositeFont

Renders text by using a default font, for example, "Times New Roman" for Latin characters.

The following example shows how to enumerate the fonts in the system font collection. The font family name of each FontFamily within SystemFontFamilies is added as an item to a combo box.

No code example is currently available or this language may not be supported.

If multiple versions of the same font family reside in the same directory, the Windows Presentation Foundation (WPF) font enumeration returns the most recent version of the font. If the version information does not provide resolution, the font with latest timestamp is returned. If the timestamp information is equivalent, the font file that is first in alphabetical order is returned.

.NET Framework

Supported in: 4, 3.5, 3.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
Was this page helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft