Export (0) Print
Expand All

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.

  NameDescription
Public methodFormattedText(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 methodFormattedText(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 methodFormattedText(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

  NameDescription
Public propertyBaselineGets the distance from the top of the first line to the baseline of the first line of a FormattedText object.
Public propertyExtentGets the distance from the topmost drawn pixel of the first line to the bottommost drawn pixel of the last line.
Public propertyFlowDirectionGets or sets the FlowDirection of a FormattedText object.
Public propertyHeightGets the distance from the top of the first line to the bottom of the last line of the FormattedText object.
Public propertyLineHeightGets the line height, or line spacing, between lines of text.
Public propertyMaxLineCountGets or sets the maximum number of lines to display. Text exceeding the MaxLineCount will not be displayed.
Public propertyMaxTextHeightGets or sets the maximum height of a text column.
Public propertyMaxTextWidthGets or sets the maximum text width (length) for a line of text.
Public propertyMinWidthGets the smallest possible text width that can fully contain the specified text content.
Public propertyOverhangAfterGets the distance from the bottom of the last line of text to the bottommost drawn pixel.
Public propertyOverhangLeadingGets the maximum distance from the leading alignment point to the leading drawn pixel of a line.
Public propertyOverhangTrailingGets the maximum distance from the trailing inked pixel to the trailing alignment point of a line.
Public propertyTextGets the string of text to be displayed.
Public propertyTextAlignmentGets or sets the alignment of text within a FormattedText object.
Public propertyTrimmingGets or sets the means by which the omission of text is indicated.
Public propertyWidthGets the width between the leading and trailing alignment points of a line, excluding any trailing white-space characters.
Public propertyWidthIncludingTrailingWhitespaceGets the width between the leading and trailing alignment points of a line, including any trailing white-space characters.
Top

  NameDescription
Public methodBuildGeometryReturns a Geometry object that represents the formatted text, including all glyphs and text decorations.
Public methodBuildHighlightGeometry(Point)Returns a Geometry object that represents the highlight bounding box of the formatted text.
Public methodBuildHighlightGeometry(Point, Int32, Int32)Returns a Geometry object that represents the highlight bounding box for a specified substring of the formatted text.
Public methodEquals(Object)Determines whether the specified Object is equal to the current Object. (Inherited from Object.)
Protected methodFinalizeAllows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.)
Public methodGetHashCodeServes as a hash function for a particular type. (Inherited from Object.)
Public methodGetMaxTextWidthsRetrieves an array of text widths. Each element in the array represents the maximum text width of sequential lines of text.
Public methodGetTypeGets the Type of the current instance. (Inherited from Object.)
Protected methodMemberwiseCloneCreates a shallow copy of the current Object. (Inherited from Object.)
Public methodSetCulture(CultureInfo)Sets the CultureInfo for the entire set of characters in the FormattedText object.
Public methodSetCulture(CultureInfo, Int32, Int32)Sets the CultureInfo for a specified subset of characters in the FormattedText object.
Public methodSetFontFamily(FontFamily)Sets the font family for a FormattedText object.
Public methodSetFontFamily(String)Sets the font family for the entire set of characters in the FormattedText object.
Public methodSetFontFamily(FontFamily, Int32, Int32)Sets the font family for a specified subset of characters in the FormattedText object.
Public methodSetFontFamily(String, Int32, Int32)Sets the font family for a specified subset of characters in the FormattedText object.
Public methodSetFontSize(Double)Sets the font size for the entire set of characters in the FormattedText object.
Public methodSetFontSize(Double, Int32, Int32)Sets the font size for a specified subset of characters in the FormattedText object.
Public methodSetFontStretch(FontStretch)Sets the font stretch value for the entire set of characters in the FormattedText object.
Public methodSetFontStretch(FontStretch, Int32, Int32)Sets the font stretch value for a specified subset of characters in the FormattedText object.
Public methodSetFontStyle(FontStyle)Sets the font style for the entire set of characters in the FormattedText object.
Public methodSetFontStyle(FontStyle, Int32, Int32)Sets the font style for a specified subset of characters in the FormattedText object.
Public methodSetFontTypeface(Typeface)Sets the font typeface for the entire set of characters in the FormattedText object.
Public methodSetFontTypeface(Typeface, Int32, Int32)Sets the font typeface for a specified subset of characters in the FormattedText object.
Public methodSetFontWeight(FontWeight)Sets the font weight for the entire set of characters in the FormattedText object.
Public methodSetFontWeight(FontWeight, Int32, Int32)Changes the FontWeight for specified text within a FormattedText object.
Public methodSetForegroundBrush(Brush)Changes the foreground Brush for an entire FormattedText object.
Public methodSetForegroundBrush(Brush, Int32, Int32)Changes the foreground Brush for specified text within a FormattedText object.
Public methodSetMaxTextWidthsSets 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 methodSetNumberSubstitution(NumberSubstitution)Sets the number substitution behavior for the entire set of characters in the FormattedText object.
Public methodSetNumberSubstitution(NumberSubstitution, Int32, Int32)Sets the number substitution behavior for specified text within a FormattedText object.
Public methodSetTextDecorations(TextDecorationCollection)Sets the TextDecorationCollection for the entire set of characters in the FormattedText object.
Public methodSetTextDecorations(TextDecorationCollection, Int32, Int32)Sets the TextDecorationCollection for specified text within a FormattedText object.
Public methodToStringReturns 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.

Community Additions

ADD
Show:
© 2014 Microsoft