This topic has not yet been rated - Rate this topic

FormattedText Class

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

System.Object
  System.Windows.Media.FormattedText

Namespace:  System.Windows.Media
Assembly:  PresentationCore (in PresentationCore.dll)
public class FormattedText

The FormattedText type exposes the following members.

  Name Description
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
  Name Description
Public property Baseline Gets the distance from the top of the first line to the baseline of the first line of a FormattedText object.
Public property Extent Gets the distance from the topmost drawn pixel of the first line to the bottommost drawn pixel of the last line.
Public property FlowDirection Gets or sets the FlowDirection of a FormattedText object.
Public property Height Gets the distance from the top of the first line to the bottom of the last line of the FormattedText object.
Public property LineHeight Gets the line height, or line spacing, between lines of text.
Public property MaxLineCount Gets or sets the maximum number of lines to display. Text exceeding the MaxLineCount will not be displayed.
Public property MaxTextHeight Gets or sets the maximum height of a text column.
Public property MaxTextWidth Gets or sets the maximum text width (length) for a line of text.
Public property MinWidth Gets the smallest possible text width that can fully contain the specified text content.
Public property OverhangAfter Gets the distance from the bottom of the last line of text to the bottommost drawn pixel.
Public property OverhangLeading Gets the maximum distance from the leading alignment point to the leading drawn pixel of a line.
Public property OverhangTrailing Gets the maximum distance from the trailing inked pixel to the trailing alignment point of a line.
Public property Text Gets the string of text to be displayed.
Public property TextAlignment Gets or sets the alignment of text within a FormattedText object.
Public property Trimming Gets or sets the means by which the omission of text is indicated.
Public property Width Gets the width between the leading and trailing alignment points of a line, excluding any trailing white-space characters.
Public property WidthIncludingTrailingWhitespace Gets the width between the leading and trailing alignment points of a line, including any trailing white-space characters.
Top
  Name Description
Public method BuildGeometry Returns 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 Finalize Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.)
Public method GetHashCode Serves as a hash function for a particular type. (Inherited from Object.)
Public method GetMaxTextWidths Retrieves an array of text widths. Each element in the array represents the maximum text width of sequential lines of text.
Public method GetType Gets the Type of the current instance. (Inherited from Object.)
Protected method MemberwiseClone Creates 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 SetMaxTextWidths Sets 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 ToString Returns a string that represents the current object. (Inherited from Object.)
Top

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.

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


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));
}


.NET Framework

Supported in: 4, 3.5, 3.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
Did you find this helpful?
(1500 characters remaining)
Community Content Add
Annotations FAQ