
New Text Application Programming Interfaces (APIs)
WPF provides several text APIs for developers to use when including text in their applications. These APIs are grouped into three categories:
Layout and user interface. The common text controls for the graphical user interface (GUI).
Lightweight text drawing. Allows you to draw text directly to objects.
Advanced text formatting. Allows you to implement a custom text engine.
Layout and User Interface
At the highest level of functionality, the text APIs provide common user interface (UI) controls such as Label, TextBlock, and TextBox. These controls provide the basic UI elements within an application, and offer an easy way to present and interact with text. Controls such as RichTextBox and PasswordBox enable more advanced or specialized text-handling. And classes such as TextRange, TextSelection, and TextPointer enable useful text manipulation. These UI controls provide properties such as FontFamily, FontSize, and FontStyle, which enable you to control the font that is used to render the text.
Using Bitmap Effects, Transforms, and Text Effects
WPF allows you to create visually interesting uses of text by uses features such as bitmap effects, transforms, and text effects. The following example shows a typical type of a drop shadow effect applied to text.
Text with a drop shadow
.jpg)
The following example shows a drop shadow effect and noise applied to text.
Text with a drop shadow and noise
.jpg)
The following example shows an outer glow effect applied to text.
Text with an outer glow effect
.jpg)
The following example shows a blur effect applied to text.
Text with a blur effect
.jpg)
The following example shows the second line of text scaled by 150% along the x-axis, and the third line of text scaled by 150% along the y-axis.
Text using a ScaleTransform
.jpg)
The following example shows text skewed along the x-axis.
Text using a SkewTransform
.jpg)
A TextEffect object is a helper object that allows you to treat text as one or more groups of characters in a text string. The following example taken from the TextEffect Sample shows an individual character being rotated. Each character is rotated independently at 1-second intervals.
Example of a rotating text effect animation
.jpg)
Using Flow Documents
In addition to the common UI controls, WPF offers a layout control for text presentation—the FlowDocument element. The FlowDocument element, in conjunction with the DocumentViewer element, provides a control for large amounts of text with varying layout requirements. Layout controls provide access to advanced typography through the Typography object and font-related properties of other UI controls.
The following example shows text content hosted in a FlowDocumentReader, which provides search, navigation, pagination, and content scaling support.
Text hosted in a FlowDocumentReader
.png)
For more information, see Documents in Windows Presentation Foundation.
Lightweight Text Drawing
You can draw text directly to WPF objects by using the DrawText method of the DrawingContext object. To use this method, you create a FormattedText object. This object allows you to draw multi-line text, in which each character in the text can be individually formatted. The functionality of the FormattedText object contains much of the functionality of the DrawText flags in the Win32 API. In addition, the FormattedText object contains functionality such as ellipsis support, in which an ellipsis is displayed when text exceeds its bounds. The following example shows text that has several formats applied to it, including a linear gradient on the second and third words.
Displayed text using FormattedText object
.jpg)
You can convert formatted text into Geometry objects, allowing you to create other types of visually interesting text. For example, you could create a Geometry object based on the outline of a text string.
Text outline using a linear gradient brush
.jpg)
The following examples illustrate several ways of creating interesting visual effects by modifying the stroke, fill, and highlight of converted text.
Example of setting stroke and fill to different colors
.jpg)
Example of an image brush applied to the stroke
.jpg)
Example of an image brush applied to the stroke and highlight
.jpg)
For more information on the FormattedText object, see Drawing Formatted Text.
Advanced Text Formatting
At the most advanced level of the text APIs, WPF offers you the ability to create custom text layout by using the TextFormatter object and other types in the System.Windows.Media.TextFormatting namespace. The TextFormatter and associated classes allow you to implement custom text layout that supports your own definition of character formats, paragraph styles, line breaking rules, and other layout features for international text. There are very few cases in which you would want to override the default implementation of the WPF text layout support. However, if you were creating a text editing control or application, you might require a different implementation than the default WPF implementation.
Unlike a traditional text API, the TextFormatter interacts with a text layout client through a set of callback methods. It requires the client to provide these methods in an implementation of the TextSource class. The following diagram illustrates the text layout interaction between the client application and TextFormatter.
Interaction between application and TextFormatter
.png)
For more details on creating custom text layout, see Advanced Text Formatting.