TextFormatter 類別

定義

使用自訂文字配置用戶端提供格式化文字和分隔文字行的服務。

public ref class TextFormatter abstract : IDisposable
public abstract class TextFormatter : IDisposable
type TextFormatter = class
    interface IDisposable
Public MustInherit Class TextFormatter
Implements IDisposable
繼承
TextFormatter
實作

範例

下列範例示範如何使用 TextFormatter 物件來產生可顯示為繪圖物件的格式化文字行。

// Create a TextFormatter object.
TextFormatter formatter = TextFormatter.Create();

// Create common paragraph property settings.
CustomTextParagraphProperties customTextParagraphProperties
    = new CustomTextParagraphProperties();

// Format each line of text from the text store and draw it.
while (textStorePosition < customTextSource.Text.Length)
{
    // Create a textline from the text store using the TextFormatter object.
    using (TextLine myTextLine = formatter.FormatLine(
        customTextSource,
        textStorePosition,
        96 * 6,
        customTextParagraphProperties,
        null))
    {
        // Draw the formatted text into the drawing context.
        myTextLine.Draw(drawingContext, linePosition, InvertAxes.None);

        // Update the index position in the text store.
        textStorePosition += myTextLine.Length;

        // Update the line position coordinate for the displayed line.
        linePosition.Y += myTextLine.Height;
    }
}
' Create a TextFormatter object.
Dim formatter As TextFormatter = TextFormatter.Create()

' Create common paragraph property settings.
Dim customTextParagraphProperties As New CustomTextParagraphProperties()

' Format each line of text from the text store and draw it.
Do While textStorePosition < customTextSource.Text.Length
    ' Create a textline from the text store using the TextFormatter object.
    Using myTextLine As TextLine = formatter.FormatLine(customTextSource, textStorePosition, 96 * 6, customTextParagraphProperties, Nothing)
        ' Draw the formatted text into the drawing context.
        myTextLine.Draw(drawingContext, linePosition, InvertAxes.None)

        ' Update the index position in the text store.
        textStorePosition += myTextLine.Length

        ' Update the line position coordinate for the displayed line.
        linePosition.Y += myTextLine.Height
    End Using
Loop

備註

TextFormatter 是 WPF 文字引擎,可提供格式化文字和中斷文字行的服務。 TextFormatter 可以處理不同的文字字元格式和段落樣式,並包含國際文字配置的支援。

不同于傳統的文字 API,會 TextFormatter 透過一組回呼方法與文字配置用戶端互動。 它要求用戶端在 類別的實作 TextSource 中提供這些方法。 下圖解說文字格式器用戶端與文字版面配置用戶端的互動。

文字配置用戶端和 TextFormatter 的圖表
文字配置用戶端與 TextFormatter 物件之間的互動

文字格式服務

TextFormatter 支援文字功能,例如 OpenType 印刷樣式,以及支援文字執行、行和段落層級功能。

下表是文字功能的主要 TextFormatter 支援服務清單。

功能 描述
背景筆刷 Brush用於背景色彩的 。 請參閱 BackgroundBrush 屬性。
基準對齊 決定線條的基準距離。 請參閱 BaselineAlignment 屬性。
字型連結 透過使用複合字型提供字型連結。
前景筆刷 Brush用於前景色彩的 。 請參閱 ForegroundBrush 屬性。
OpenType 進階 OpenType 印刷樣式功能,例如內容替代專案和標準連字。 TextRunTypographyProperties請參閱類別屬性。
字樣 決定字樣,以及粗細、樣式和延展。 請參閱 Typeface 屬性。
文字裝飾 提供物件的支援 TextDecoration ,例如底線或刪除線。 請參閱 TextDecorations 屬性。
文字效果 提供 物件的支援 TextEffect 。 請參閱 TextEffects 屬性。

下表是段落層級功能的主要 TextFormatter 支援服務清單。

功能 描述
Flow方向 Flow段落的方向 - 由左至右或由右至左。 請參閱 FlowDirection 屬性。
行縮排 段落中每一行的縮排。 請參閱 Indent 屬性。
標記 段落中第一行的標記特性。 請參閱 TextMarkerProperties 屬性。
段落縮排 段落中第一行的縮排。 請參閱 ParagraphIndent 屬性。
定位點 包含索引標籤對齊和索引標籤領導者的支援。
寬度 判斷最小和最大段落寬度。 FormatMinMaxParagraphWidth請參閱 方法。
自動換行 判斷文字在到達流程邊緣時如何換行。 請參閱 TextWrapping 屬性。

下表是行層級功能的主要 TextFormatter 支援服務清單。

功能 描述
對齊 文字對齊 - 左、右、置中或對齊。 請參閱 TextAlignment 屬性。
行高 線條的高度。 請參閱 Height 屬性。
線條折迭 線條折迭支援。 Collapse請參閱 方法。
黑色方塊度量 表示用於配置裝置字型字元的度量。 請參閱 CharacterMetrics 類別。
插入號支援 插入號流覽和編輯支援。 TextLine請參閱字元點擊屬性,例如 GetNextCaretCharacterHitGetPreviousCaretCharacterHit

下表是文字執行層級功能的主要 TextFormatter 支援服務清單。

功能 描述
隱藏的執行 看不到的字元範圍。 TextHidden請參閱 物件。
中斷 分行符號和段落。

建構函式

TextFormatter()

初始化 TextFormatter 類別的新執行個體。

方法

Create()

建立 TextFormatter 類別的新執行個體。 此為靜態方法。

Create(TextFormattingMode)

使用指定的格式化模式,建立 TextFormatter 類別的新執行個體。 此為靜態方法。

Dispose()

釋放 TextFormatter 物件使用的所有 Managed 和 Unmanaged 資源。

Equals(Object)

判斷指定的物件是否等於目前的物件。

(繼承來源 Object)
FormatLine(TextSource, Int32, Double, TextParagraphProperties, TextLineBreak)

建立用於格式化和顯示文件內容的 TextLine

FormatLine(TextSource, Int32, Double, TextParagraphProperties, TextLineBreak, TextRunCache)

建立用於格式化和顯示文件內容的 TextLine

FormatMinMaxParagraphWidth(TextSource, Int32, TextParagraphProperties)

傳回值,代表可完整內含指定之文字內容的最小及最大可能段落的寬度。

FormatMinMaxParagraphWidth(TextSource, Int32, TextParagraphProperties, TextRunCache)

傳回值,代表可完整內含指定之文字內容的最小及最大可能段落的寬度。

GetHashCode()

做為預設雜湊函式。

(繼承來源 Object)
GetType()

取得目前執行個體的 Type

(繼承來源 Object)
MemberwiseClone()

建立目前 Object 的淺層複製。

(繼承來源 Object)
ToString()

傳回代表目前物件的字串。

(繼承來源 Object)

適用於

另請參閱