TextFormatter 类

定义

使用自定义文本布局客户端提供用于为文本设置格式及断开文本行的服务。Provides services for formatting text and breaking text lines using a custom text layout client.

public ref class TextFormatter abstract : IDisposable
public abstract class TextFormatter : IDisposable
type TextFormatter = class
    interface IDisposable
Public MustInherit Class TextFormatter
Implements IDisposable
继承
TextFormatter
实现

示例

下面的示例演示如何使用 TextFormatter 对象生成可显示为绘图对象的格式化文本行。The following example shows how to use a TextFormatter object to generate formatted lines of text that can be displayed as drawing objects.

// 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 是提供用于设置文本格式和断开文本线条的服务的 WPFWPF 文本引擎。TextFormatter is the WPFWPF text engine that provides services for formatting text and breaking text lines. TextFormatter 可以处理不同的文本字符格式和段落样式,并提供对国际文本布局的支持。TextFormatter can handle different text character formats and paragraph styles, and includes support for international text layout.

与传统的文本 API 不同,TextFormatter 通过一组回调方法与文本布局客户端进行交互。Unlike a traditional text API, the TextFormatter interacts with a text layout client through a set of callback methods. 它要求客户端在 TextSource 类的实现中提供这些方法。It requires the client to provide these methods in an implementation of the TextSource class. 下图说明了文本格式化程序客户端和文本布局客户端的交互。The following diagram illustrates the interaction of the text formatter client and the text layout client.

文本布局客户端和 TextFormatter 示意图Diagram of text layout client and TextFormatter
文本布局客户端与 System.windows.media.textformatting.textformatter> 对象之间的交互Interaction between the text layout client and the TextFormatter object

文本格式设置服务Text Formatting Services

TextFormatter 提供对文本功能(如 OpenType 版式)的支持,以及对文本运行、线条和段落级别功能的支持。TextFormatter provides support for text features, such as OpenType typography, as well as support for text run, line, and paragraph level features.

下表是 TextFormatter 支持的文本功能服务的密钥列表。The following table is a list of the key TextFormatter supported services for text features.

功能Feature 说明Description
背景画笔Background brush 用于背景色的 BrushThe Brush that is used for the background color. 请参阅 BackgroundBrush 属性。See the BackgroundBrush property.
基线对齐Baseline alignment 确定线条的基线距离。Determines the baseline distance of the line. 请参阅 BaselineAlignment 属性。See the BaselineAlignment property.
字体链接Font linking 通过使用复合字体提供字体链接。Provides font linking through the use of composite fonts.
前景画笔Foreground brush 用于前景色的 BrushThe Brush that is used for the foreground color. 请参阅 ForegroundBrush 属性。See the ForegroundBrush property.
OpenTypeOpenType 高级 OpenType 排字功能,如上下文备用字和标准连字。Advanced OpenType typographic features, such contextual alternates and standard ligatures. 请参阅 TextRunTypographyProperties 类属性。See the TextRunTypographyProperties class properties.
字样Typeface 确定字样、粗细、样式和拉伸。Determines the typeface, as well as, weight, style, and stretch. 请参阅 Typeface 属性。See the Typeface property.
文本修饰Text decoration 提供对 TextDecoration 对象(如下划线或删除线)的支持。Provides support for TextDecoration objects, such as underline or strikethrough. 请参阅 TextDecorations 属性。See the TextDecorations property.
文本效果Text effects TextEffect 对象提供支持。Provides support for TextEffect objects. 请参阅 TextEffects 属性。See the TextEffects property.

下表列出了 TextFormatter 支持的段落级功能的服务的密钥列表。The following table is a list of the key TextFormatter supported services for paragraph-level features.

功能Feature 说明Description
流方向Flow direction 从左到右或从右到左的段落的流动方向。Flow direction of the paragraph - left-to-right, or right-to-left. 请参阅 FlowDirection 属性。See the FlowDirection property.
行缩进Line indentation 段落中每行的缩进量。Indentation for each line in the paragraph. 请参阅 Indent 属性。See the Indent property.
标记Marker 段落中第一行的标记特征。Marker characteristics of the first line in the paragraph. 请参阅 TextMarkerProperties 属性。See the TextMarkerProperties property.
段落缩进Paragraph indentation 段落中第一行的缩进量。Indentation for the first line in the paragraph. 请参阅 ParagraphIndent 属性。See the ParagraphIndent property.
选项卡Tabs 支持制表符对齐和制表符前导符。Includes support for tab alignment and tab leader.
宽度Width 确定最小和最大段落宽度。Determine minimum and maximum paragraph width. 请参阅 FormatMinMaxParagraphWidth 方法。See the FormatMinMaxParagraphWidth methods.
自动换行Word wrap 确定文本在到达流边缘时如何换行。Determine how text wraps when it reaches the flow edge. 请参阅 TextWrapping 属性。See the TextWrapping property.

下表列出了用于行级功能的支持服务 TextFormatterThe following table is a list of the key TextFormatter supported services for line-level features.

功能Feature 说明Description
对齐Alignment 文本对齐方式(左对齐、右对齐、居中或两端对齐)。Text alignment - left, right, centered, or justified. 请参阅 TextAlignment 属性。See the TextAlignment property.
行高Line height 线条的高度。Height of the line. 请参阅 Height 属性。See the Height property.
行折叠Line collapsing 支持行折叠。Line collapsing support. 请参阅 Collapse 方法。See the Collapse method.
黑盒度量Black box measurement 表示用于对设备字体字符进行布局的规格。Represents the metrics used to lay out a character in a device font. 请参阅 CharacterMetrics 类。See the CharacterMetrics class.
插入符号支持Caret support 插入点导航和编辑支持。Caret navigation and editing support. 请参阅 TextLine 字符命中属性,如 GetNextCaretCharacterHitGetPreviousCaretCharacterHitSee the TextLine character hit properties, such as GetNextCaretCharacterHit and GetPreviousCaretCharacterHit.

下表列出了 TextFormatter 支持的文本运行级功能的服务的密钥列表。The following table is a list of the key TextFormatter supported services for text run-level features.

功能Feature 说明Description
隐藏运行Hidden run 不可见的字符范围。Range of characters that are not visible. 请参阅 TextHidden 对象。See the TextHidden object.
正在中断Breaking 行和段落中断。Line and paragraph breaking.

构造函数

TextFormatter()

初始化 TextFormatter 类的新实例。Initializes a new instance of the TextFormatter class.

方法

Create()

创建 TextFormatter 类的新实例。Creates a new instance of the TextFormatter class. 这是静态方法。This is a static method.

Create(TextFormattingMode)

使用指定的格式设置模式创建 TextFormatter 类的新实例。Creates a new instance of the TextFormatter class with the specified formatting mode. 这是静态方法。This is a static method.

Dispose()

释放由 TextFormatter 对象使用的所有托管资源和非托管资源。Releases all managed and unmanaged resources used by the TextFormatter object.

Equals(Object)

确定指定的对象是否等于当前对象。Determines whether the specified object is equal to the current object.

(继承自 Object)
FormatLine(TextSource, Int32, Double, TextParagraphProperties, TextLineBreak)

创建一个 TextLine,用来为文档内容设置格式并显示文档内容。Creates a TextLine that is used for formatting and displaying document content.

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

创建一个 TextLine,用来为文档内容设置格式并显示文档内容。Creates a TextLine that is used for formatting and displaying document content.

FormatMinMaxParagraphWidth(TextSource, Int32, TextParagraphProperties)

返回一个值,该值表示可完全包含指定文本内容的最小和最大可能的段落宽度。Returns a value that represents the smallest and largest possible paragraph width that can fully contain the specified text content.

FormatMinMaxParagraphWidth(TextSource, Int32, TextParagraphProperties, TextRunCache)

返回一个值,该值表示可完全包含指定文本内容的最小和最大可能的段落宽度。Returns a value that represents the smallest and largest possible paragraph width that can fully contain the specified text content.

GetHashCode()

用作默认哈希函数。Serves as the default hash function.

(继承自 Object)
GetType()

获取当前实例的 TypeGets the Type of the current instance.

(继承自 Object)
MemberwiseClone()

创建当前 Object 的浅表副本。Creates a shallow copy of the current Object.

(继承自 Object)
ToString()

返回一个表示当前对象的 string。Returns a string that represents the current object.

(继承自 Object)

适用于

另请参阅