Skip to main content
.NET Framework Class Library
FormattedText Class

Provides low-level control for drawing text in Windows Presentation Foundation (WPF) applications.

Inheritance Hierarchy
SystemObject
  System.Windows.MediaFormattedText

Namespace:   System.Windows.Media
Assembly:  PresentationCore (in PresentationCore.dll)
Syntax
Public Class FormattedText
public class FormattedText
public ref class FormattedText
type FormattedText =  class end
public class FormattedText

The FormattedText type exposes the following members.

Constructors
  NameDescription
Public method FormattedText(String, CultureInfo, FlowDirection, Typeface, Double, Brush)Initializes a new instance of the FormattedText class with the specified text, culture, flow direction, typeface, font size, and brush.
Public method FormattedText(String, CultureInfo, FlowDirection, Typeface, Double, Brush, NumberSubstitution)Initializes a new instance of the FormattedText class with the specified text, culture, flow direction, typeface, font size, brush, and number substitution behavior.
Public method FormattedText(String, CultureInfo, FlowDirection, Typeface, Double, Brush, NumberSubstitution, TextFormattingMode)Initializes a new instance of the FormattedText class with the specified text, culture, flow direction, typeface, font size, brush, number substitution behavior, and text formatting mode.
Top
Properties
  NameDescription
Public property BaselineGets the distance from the top of the first line to the baseline of the first line of a FormattedText object.
Public property ExtentGets the distance from the topmost drawn pixel of the first line to the bottommost drawn pixel of the last line.
Public property FlowDirectionGets or sets the FlowDirection of a FormattedText object.
Public property HeightGets the distance from the top of the first line to the bottom of the last line of the FormattedText object.
Public property LineHeightGets the line height, or line spacing, between lines of text.
Public property MaxLineCountGets or sets the maximum number of lines to display. Text exceeding the MaxLineCount will not be displayed.
Public property MaxTextHeightGets or sets the maximum height of a text column.
Public property MaxTextWidthGets or sets the maximum text width (length) for a line of text.
Public property MinWidthGets the smallest possible text width that can fully contain the specified text content.
Public property OverhangAfterGets the distance from the bottom of the last line of text to the bottommost drawn pixel.
Public property OverhangLeadingGets the maximum distance from the leading alignment point to the leading drawn pixel of a line.
Public property OverhangTrailingGets the maximum distance from the trailing inked pixel to the trailing alignment point of a line.
Public property TextGets the string of text to be displayed.
Public property TextAlignmentGets or sets the alignment of text within a FormattedText object.
Public property TrimmingGets or sets the means by which the omission of text is indicated.
Public property WidthGets the width between the leading and trailing alignment points of a line, excluding any trailing white-space characters.
Public property WidthIncludingTrailingWhitespaceGets the width between the leading and trailing alignment points of a line, including any trailing white-space characters.
Top
Methods
  NameDescription
Public method BuildGeometryReturns a Geometry object that represents the formatted text, including all glyphs and text decorations.
Public method BuildHighlightGeometry(Point)Returns a Geometry object that represents the highlight bounding box of the formatted text.
Public method BuildHighlightGeometry(Point, Int32, Int32)Returns a Geometry object that represents the highlight bounding box for a specified substring of the formatted text.
Public method Equals(Object)Determines whether the specified object is equal to the current object. (Inherited from Object.)
Protected method FinalizeAllows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.)
Public method GetHashCodeServes as the default hash function. (Inherited from Object.)
Public method GetMaxTextWidthsRetrieves an array of text widths. Each element in the array represents the maximum text width of sequential lines of text.
Public method GetTypeGets the Type of the current instance. (Inherited from Object.)
Protected method MemberwiseCloneCreates a shallow copy of the current Object. (Inherited from Object.)
Public method SetCulture(CultureInfo)Sets the CultureInfo for the entire set of characters in the FormattedText object.
Public method SetCulture(CultureInfo, Int32, Int32)Sets the CultureInfo for a specified subset of characters in the FormattedText object.
Public method SetFontFamily(FontFamily)Sets the font family for a FormattedText object.
Public method SetFontFamily(String)Sets the font family for the entire set of characters in the FormattedText object.
Public method SetFontFamily(FontFamily, Int32, Int32)Sets the font family for a specified subset of characters in the FormattedText object.
Public method SetFontFamily(String, Int32, Int32)Sets the font family for a specified subset of characters in the FormattedText object.
Public method SetFontSize(Double)Sets the font size for the entire set of characters in the FormattedText object.
Public method SetFontSize(Double, Int32, Int32)Sets the font size for a specified subset of characters in the FormattedText object.
Public method SetFontStretch(FontStretch)Sets the font stretch value for the entire set of characters in the FormattedText object.
Public method SetFontStretch(FontStretch, Int32, Int32)Sets the font stretch value for a specified subset of characters in the FormattedText object.
Public method SetFontStyle(FontStyle)Sets the font style for the entire set of characters in the FormattedText object.
Public method SetFontStyle(FontStyle, Int32, Int32)Sets the font style for a specified subset of characters in the FormattedText object.
Public method SetFontTypeface(Typeface)Sets the font typeface for the entire set of characters in the FormattedText object.
Public method SetFontTypeface(Typeface, Int32, Int32)Sets the font typeface for a specified subset of characters in the FormattedText object.
Public method SetFontWeight(FontWeight)Sets the font weight for the entire set of characters in the FormattedText object.
Public method SetFontWeight(FontWeight, Int32, Int32)Changes the FontWeight for specified text within a FormattedText object.
Public method SetForegroundBrush(Brush)Changes the foreground Brush for an entire FormattedText object.
Public method SetForegroundBrush(Brush, Int32, Int32)Changes the foreground Brush for specified text within a FormattedText object.
Public method SetMaxTextWidthsSets an array of maximum text widths within the FormattedText, on a per-line basis. Each element in the array represents the maximum text width of sequential lines of text.
Public method SetNumberSubstitution(NumberSubstitution)Sets the number substitution behavior for the entire set of characters in the FormattedText object.
Public method SetNumberSubstitution(NumberSubstitution, Int32, Int32)Sets the number substitution behavior for specified text within a FormattedText object.
Public method SetTextDecorations(TextDecorationCollection)Sets the TextDecorationCollection for the entire set of characters in the FormattedText object.
Public method SetTextDecorations(TextDecorationCollection, Int32, Int32)Sets the TextDecorationCollection for specified text within a FormattedText object.
Public method ToStringReturns a string that represents the current object. (Inherited from Object.)
Top
Remarks

The FormattedText object allows you to draw multi-line text, in which each character in the text can be individually formatted. The following example shows text that has several formats applied to it.

Example of a formatted text string

Text displayed using FormattedText object

In terms of text features in WPF, FormattedText is considered "low-level" because it processes text as graphical elements. Other aspects of text in WPF handle text in the context of controls that are dedicated to text ( TextBlock, TextBox), implement the flow document model (see Flow Document Overview), or support the XPS document model (see Documents in WPF).

Many of the setter methods in FormattedText have similarities to attached properties that are supported by TextElement, but the TextElement attached properties apply to the higher-level text support either for flow or XPS.

Examples

The following example creates a FormattedText object and then applies several formatting styles to the text.

		Protected Overrides Sub OnRender(ByVal drawingContext As DrawingContext)
			Dim testString As String = "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor"

			' Create the initial formatted text string.
			Dim formattedText As New FormattedText(testString, CultureInfo.GetCultureInfo("en-us"), FlowDirection.LeftToRight, New Typeface("Verdana"), 32, Brushes.Black)

			' Set a maximum width and height. If the text overflows these values, an ellipsis "..." appears.
			formattedText.MaxTextWidth = 300
			formattedText.MaxTextHeight = 240

			' Use a larger font size beginning at the first (zero-based) character and continuing for 5 characters.
			' The font size is calculated in terms of points -- not as device-independent pixels.
			formattedText.SetFontSize(36 * (96.0 / 72.0), 0, 5)

			' Use a Bold font weight beginning at the 6th character and continuing for 11 characters.
			formattedText.SetFontWeight(FontWeights.Bold, 6, 11)

			' Use a linear gradient brush beginning at the 6th character and continuing for 11 characters.
			formattedText.SetForegroundBrush(New LinearGradientBrush(Colors.Orange, Colors.Teal, 90.0), 6, 11)

			' Use an Italic font style beginning at the 28th character and continuing for 28 characters.
			formattedText.SetFontStyle(FontStyles.Italic, 28, 28)

			' Draw the formatted text string to the DrawingContext of the control.
			drawingContext.DrawText(formattedText, New Point(10, 0))
		End Sub
protected override void OnRender(DrawingContext drawingContext)
{
    string testString = "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor";

    // Create the initial formatted text string.
    FormattedText formattedText = new FormattedText(
        testString,
        CultureInfo.GetCultureInfo("en-us"),
        FlowDirection.LeftToRight,
        new Typeface("Verdana"),
        32,
        Brushes.Black);

    // Set a maximum width and height. If the text overflows these values, an ellipsis "..." appears.
    formattedText.MaxTextWidth = 300;
    formattedText.MaxTextHeight = 240;

    // Use a larger font size beginning at the first (zero-based) character and continuing for 5 characters. 
    // The font size is calculated in terms of points -- not as device-independent pixels.
    formattedText.SetFontSize(36 * (96.0 / 72.0), 0, 5);

    // Use a Bold font weight beginning at the 6th character and continuing for 11 characters.
    formattedText.SetFontWeight(FontWeights.Bold, 6, 11);

    // Use a linear gradient brush beginning at the 6th character and continuing for 11 characters.
    formattedText.SetForegroundBrush(
                            new LinearGradientBrush(
                            Colors.Orange,
                            Colors.Teal,
                            90.0),
                            6, 11);

    // Use an Italic font style beginning at the 28th character and continuing for 28 characters.
    formattedText.SetFontStyle(FontStyles.Italic, 28, 28);

    // Draw the formatted text string to the DrawingContext of the control.
    drawingContext.DrawText(formattedText, new Point(10, 0));
}
Version Information

.NET Framework

Supported in: 4.6, 4.5, 4, 3.5, 3.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1
Thread Safety
Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.