FontFamily FontFamily FontFamily FontFamily Class

定義

代表一系列的相關字型。Represents a family of related fonts.

public ref class FontFamily
[System.ComponentModel.TypeConverter(typeof(System.Windows.Media.FontFamilyConverter))]
[System.Windows.Localizability(System.Windows.LocalizationCategory.Font)]
public class FontFamily
type FontFamily = class
Public Class FontFamily
繼承
FontFamilyFontFamilyFontFamilyFontFamily
屬性

備註

字型系列是共用相同系列名稱的一組字樣, 例如「Times New Roman」, 但功能不同。A font family is a set of typefaces that share the same family name, such as "Times New Roman", but that differ in features. 這些功能的差異Style包括: 斜體和Weight(例如粗體)。These feature differences include Style, such as italic, and Weight, such as bold.

![]字型(~/add/media/fonts.PNG "")字型Fonts
屬於「新羅馬字」字型系列成員的字樣範例Example of typefaces that are members of the "Times New Roman" font family

大部分使用者介面 (UI)user interface (UI)的元素 ( Button例如和TextBlock) 都會提供FontFamily屬性, 可用來指定控制項文字內容的字型。Most 使用者介面 (UI)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. 您可以使用FontFamily值來設定該屬性, 以定義字型。You define the font by setting that property with a FontFamily value. 下列範例示範如何在Extensible Application Markup Language (XAML)Extensible Application Markup Language (XAML)程式碼的和中參考字型。The following examples show how to reference a font, in Extensible Application Markup Language (XAML)Extensible Application Markup Language (XAML) and in code.

myTextBlock.FontFamily = new FontFamily("Comic Sans MS");
myTextBlock.FontFamily = New FontFamily("Comic Sans MS")
<TextBlock FontFamily="Comic Sans MS">Hello, world</TextBlock>

在上述範例中, 參考的字型 "漫畫 Sans MS" 是由其易記名稱參考。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

Windows Presentation Foundation (WPF)Windows Presentation Foundation (WPF)應用程式可以指定目錄, 而不是包含系統字型集合的目錄, 用於解析字型參考。A Windows Presentation Foundation (WPF)Windows Presentation Foundation (WPF) application can specify a directory, other than the directory that contains the systems font collection, for resolving font references. 字型的易記名稱可以指定用來解析字型uniform resource identifier (URI)參考的絕對值, 如下列Extensible Application Markup Language (XAML)Extensible Application Markup Language (XAML)和程式碼範例所示。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)Extensible Application Markup Language (XAML) and code examples.

// Create a new FontFamily object, using an absolute URI reference.
myTextBlock.FontFamily = new FontFamily("file:///d:/MyFonts/#Pericles Light");
' Create a new FontFamily object, using an absolute URI reference.
myTextBlock.FontFamily = New FontFamily("file:///d:/MyFonts/#Pericles Light")
<TextBlock FontFamily="file:///d:/MyFonts/#Pericles Light">
  Aegean Sea
</TextBlock>

字型的易記名稱也可以指定相對值URI , 此值需要基底URI來解析字型參考。The friendly name of the font can also specify a relative URI value, which requires a base URI to resolve the font reference. FontFamily物件的URI屬性會對應至基底值。 BaseUriThe BaseUri property of the FontFamily object corresponds to the base URI value. 下列程式碼範例顯示如何建立由基底URI值和相對值URI組成的字型參考。The following code example shows how to create a font reference that is composed of a base URI value and a relative URI value.

// Create a new FontFamily object, using a base URI reference and a relative URI reference.
myTextBlock.FontFamily = new FontFamily(new Uri("file:///d:/MyFonts/"), "./#Pericles Light");
' Create a new FontFamily object, using a base URI reference and a relative URI reference.
myTextBlock.FontFamily = New FontFamily(New Uri("file:///d:/MyFonts/"), "./#Pericles Light")

當您參考封裝為URI應用程式一部分的字型時, 可以使用基底值。You can use a base URI value when you reference a font that is packaged as part of the application. 例如, 基底URI值可以是「 URIpack://application」, 這可讓您參考封裝為應用程式資源的字型。For example, the base URI value can be a "pack://application" URI, which lets you reference fonts that are packaged as application resources. 下列程式碼範例顯示由基底URI值和相對值URI組成的字型參考。The following code example shows a font reference that is composed of a base URI value and a relative URI value.

// The font resource reference includes the base URI reference (application directory level),
// and a relative URI reference.
myTextBlock.FontFamily = new FontFamily(new Uri("pack://application:,,,/"), "./resources/#Pericles Light");
' The font resource reference includes the base URI reference (application directory level),
' and a relative URI reference.
myTextBlock.FontFamily = New FontFamily(New Uri("pack://application:,,,/"), "./resources/#Pericles Light")

當指定為標記中的屬性時, 基底URI值一律是隱含的, 其XAMLXAML值為URI頁面的。 FontFamilyWhen a FontFamily is specified as an attribute in markup, the base URI value is always implied - its value is the URI of the XAMLXAML page. 暗含的基URI底值會與易記名稱URI字串中的相對值搭配使用, 以取得字型的位置。The implied base URI value is used with the relative URI value in the friendly name string to obtain the location of the font. 在下列Extensible Application Markup Language (XAML)Extensible Application Markup Language (XAML)範例中, 請注意相對值URI會使用 "./" 標記法, 這表示基底URI值的「在目前資料夾中」。In the following Extensible Application Markup Language (XAML)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>

WPFWPF應用程式可以將字型封裝為內容專案、資源專案或程式庫資源專案。A WPFWPF 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.

WPFWPF中, 字型回溯機制會使用預設的 fallback 字型系列「全域使用者介面」作為替代字型。InWPFWPF, the font fallback mechanism uses the default fallback font family, "Global User Interface", as the substitute font. 此字型會定義為複合字型, 其檔案名為 "GlobalUserInterface. CompositeFont"。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.

字型WPFWPF回溯機制取代了先前Win32Win32的字型替換技術。The WPFWPF font fallback mechanism replaces previous Win32Win32 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. 當您建立FontFamily物件時, 請String為參數提供多個字型系列名稱 (以逗號分隔), 例如 "漫畫 Sans MS, Verdana"。When you create a FontFamily object, provide multiple font family names, separated by commas, for the String parameter, such as "Comic Sans MS, Verdana". 在此情況下, 如果無法使用來自 "漫畫 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. 如果 "漫畫 Sans MS" 或 "Verdana" 都沒有必要的字元, 則會使用字樣的回溯字型系列, 預設為「全域使用者介面」。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.

下列範例示範如何在Extensible Application Markup Language (XAML)Extensible Application Markup Language (XAML)程式碼的和中定義字型回復序列。The following examples show how to define a font fallback sequence, in Extensible Application Markup Language (XAML)Extensible Application Markup Language (XAML) and in code.

myTextBlock.FontFamily = new FontFamily("Comic Sans MS, Verdana");
myTextBlock.FontFamily = New FontFamily("Comic Sans MS, Verdana")
<TextBlock FontFamily="Comic Sans MS, Verdana">Hello, world</TextBlock>

回溯序列中的任何一種字型都可以指定字型位置。Any one of the fonts in the fallback sequence can specify font locations. 在下列範例中, 會將 "Pericles Light" 當做應用程式資源來參考, 並將 "Verdana" 當做系統字型集合成員來參考。In the following examples, "Pericles Light" is referenced as an application resource, and "Verdana" is referenced as a system font collection member.

myTextBlock.FontFamily = new FontFamily(new Uri("pack://application:,,,/"), "./resources/#Pericles Light, Verdana");
myTextBlock.FontFamily = New FontFamily(New Uri("pack://application:,,,/"), "./resources/#Pericles Light, Verdana")
<TextBlock FontFamily="./resources/#Pericles Light, Verdana">Aegean Sea</TextBlock>

複合字型Composite Fonts

WPFWPF平臺提供了一種複合字型功能, 可讓您結構完整範圍的多語系字型, 並避免顯示遺漏的字元。The WPFWPF platform provides a composite font feature to allow the construction of full range multilingual fonts, and to avoid displaying missing glyphs. 複合Win32Win32字型會取代字型連結、字型替換、字型系結、字型關聯和使用者定義字元 (EUDC) 機制。Composite fonts replace the Win32Win32 font linking, font fallback, font binding, font association, and end-user-defined characters (EUDC) mechanisms.

應用程式可以透過FontFamilyTypeface構造函式使用複合字型系列, 就像任何其他字型系列一樣。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. 這個檔案可以儲存在預設WindowsWindows字型目錄中, 就像使用任何其他已安裝的字型一樣, 或是在依名稱參考家族時包含它URI , 以在任何位置參考。This file can be stored in the default WindowsWindows 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.

下列範例顯示中的字型系列標記。CompositeFont "檔案。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>

下列四個複合字型會在 WPF 安裝的預設 Windows 字型目錄中顯示。The following four composite fonts appear in the default Windows font directory as part of the WPF installation.

字型Font 注意Notes
GlobalMonospace.CompositeFontGlobalMonospace.CompositeFont 使用等寬字型來轉譯文字, 例如, 拉丁文字元的 "幼圓 New"。Renders text by using a monospace font, for example, "Courier New" for Latin characters.
GlobalSanSerif.CompositeFontGlobalSanSerif.CompositeFont 使用 sans serif 字型來轉譯文字, 例如, "Arial" 表示拉丁字元。Renders text by using a sans serif font, for example, "Arial" for Latin characters.
GlobalSerif.CompositeFontGlobalSerif.CompositeFont 使用 serif 字型來轉譯文字, 例如, 拉丁文字元的 "Times New Roman"。Renders text by using a serif font, for example, "Times New Roman" for Latin characters.
GlobalUserInterface.CompositeFontGlobalUserInterface.CompositeFont 使用預設字型轉譯文字, 例如, 拉丁文字元的 "Times New Roman"。Renders text by using a default font, for example, "Times New Roman" for Latin characters.

XAML Attribute UsageXAML Attribute Usage

<object FontFamily="fontFamilyName"/>  
- or -  
<object FontFamily="fontFamilyNamesList"/>  
- or -  
<object FontFamily="fontFamilyFolderReference"/>  
- or -  
<object FontFamily="fontFamilyUriReference"/>  

XAML 值XAML Values

fontFamilyNamefontFamilyName
指定字型系列名稱的字串。A string specifying a font family name. 例如,"Arial""Century Gothic"For example, "Arial" or "Century Gothic".

fontFamilyNamesListfontFamilyNamesList
指定多個字型系列名稱的字串, 每個都以逗號分隔 (會忽略逗號後面的任何空白字元)。A string specifying multiple font family names, each separated by a comma (any white space 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. 例如, "Arial, Century Gothic"指定以 Arial 作為主要字型家族, 並將世紀哥特為「回溯字型家族」。For example, "Arial, Century Gothic" specifies Arial as the primary font family, with Century Gothic as the fallback font family.

fontFamilyFolderReferencefontFamilyFolderReference
字串, 指定包含字型的資料夾, 以及字型家族名稱。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. 例如,"Custom Fonts\#My Custom Font"For example, "Custom Fonts\#My Custom Font".

fontFamilyUriReferencefontFamilyUriReference
字串, 指定uniform resource identifier (URI)字型的, 連同字型系列名稱。A string specifying a uniform resource identifier (URI) for the font, along with a font family name. URI和字型系列名稱是以 # 字元分隔。The URI and font family name are delimited by a # character. 例如,"http://MyFontServer/Fonts/#My Custom Font"For example, "http://MyFontServer/Fonts/#My Custom Font".

建構函式

FontFamily() FontFamily() FontFamily() FontFamily()

初始化匿名 FontFamily 類別的新執行個體。Initializes a new instance of an anonymous FontFamily class.

FontFamily(String) FontFamily(String) FontFamily(String) FontFamily(String)

從指定的字型系列名稱初始化 FontFamily 類別的新執行個體。Initializes a new instance of the FontFamily class from the specified font family name.

FontFamily(Uri, String) FontFamily(Uri, String) FontFamily(Uri, String) FontFamily(Uri, String)

從指定的字型系列名稱和選擇性基底uniform resource identifier (URI) 值,初始化 FontFamily 類別的新執行個體。Initializes a new instance of the FontFamily class from the specified font family name and an optional base uniform resource identifier (URI) value.

屬性

Baseline Baseline Baseline Baseline

取得或設定基準線與字元儲存格頂端之間的距離。Gets or sets the distance between the baseline and the character cell top.

BaseUri BaseUri BaseUri BaseUri

取得或設定用來解析字型系列名稱的基底uniform resource identifier (URI)。Gets the base uniform resource identifier (URI) that is used to resolve a font family name.

FamilyMaps FamilyMaps FamilyMaps FamilyMaps

取得 FontFamilyMap 物件的集合。Gets the collection of FontFamilyMap objects.

FamilyNames FamilyNames FamilyNames FamilyNames

取得字串和 CultureInfo 值的集合,這些字串和值表示 FontFamily 物件的字型系列名稱。Gets a collection of strings and CultureInfo values that represent the font family names of the FontFamily object.

FamilyTypefaces FamilyTypefaces FamilyTypefaces FamilyTypefaces

取得 FontFamily 物件的字樣集合。Gets a collection of typefaces for the FontFamily object.

LineSpacing LineSpacing LineSpacing LineSpacing

取得或設定 FontFamily 物件的行距值。Gets or sets the line spacing value for the FontFamily object. 行距是當使用這個字型的文字時,基準線與基準線之間的建議距離,以相對於 em 大小的大小表示。The line spacing is the recommended baseline-to-baseline distance for the text in this font relative to the em size.

Source Source Source Source

取得用來建構 FontFamily 物件的字型家族名稱。Gets the font family name that is used to construct the FontFamily object.

方法

Equals(Object) Equals(Object) Equals(Object) Equals(Object)

取得值,這個值表示目前的字型家族物件與指定的字型家族物件是否相同。Gets a value that indicates whether the current font family object and the specified font family object are the same.

GetHashCode() GetHashCode() GetHashCode() GetHashCode()

作為 FontFamily 的雜湊函式。Serves as a hash function for FontFamily. 它適合用於雜湊演算法和諸如雜湊資料表這類的資料結構。It is suitable for use in hashing algorithms and data structures such as a hash table.

GetType() GetType() GetType() GetType()

取得目前執行個體的 TypeGets the Type of the current instance.

(Inherited from Object)
GetTypefaces() GetTypefaces() GetTypefaces() GetTypefaces()

傳回 Typeface 物件的集合,這些物件表示預設系統字型位置中的字樣。Returns a collection of Typeface objects that represent the type faces in the default system font location.

MemberwiseClone() MemberwiseClone() MemberwiseClone() MemberwiseClone()

建立目前 Object 的淺層複本 (Shallow Copy)。Creates a shallow copy of the current Object.

(Inherited from Object)
ToString() ToString() ToString() ToString()

傳回 Source 屬性值。Returns the value of the Source property.

適用於

另請參閱