FormattedText FormattedText FormattedText FormattedText Class

Definition

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

public ref class FormattedText
public class FormattedText
type FormattedText = class
Public Class FormattedText
Inheritance
FormattedTextFormattedTextFormattedTextFormattedText

Examples

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

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.

Text displayed using FormattedText object
Example of a formatted text string

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.

Constructors

FormattedText(String, CultureInfo, FlowDirection, Typeface, Double, Brush) FormattedText(String, CultureInfo, FlowDirection, Typeface, Double, Brush) FormattedText(String, CultureInfo, FlowDirection, Typeface, Double, Brush) 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.

FormattedText(String, CultureInfo, FlowDirection, Typeface, Double, Brush, Double) FormattedText(String, CultureInfo, FlowDirection, Typeface, Double, Brush, Double) FormattedText(String, CultureInfo, FlowDirection, Typeface, Double, Brush, Double) FormattedText(String, CultureInfo, FlowDirection, Typeface, Double, Brush, Double)

Initializes a new instance of the FormattedText class with the specified text, culture, flow direction, typeface, font size, foreground brush, and pixelsPerDip value.

FormattedText(String, CultureInfo, FlowDirection, Typeface, Double, Brush, NumberSubstitution) FormattedText(String, CultureInfo, FlowDirection, Typeface, Double, Brush, NumberSubstitution) FormattedText(String, CultureInfo, FlowDirection, Typeface, Double, Brush, NumberSubstitution) 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.

FormattedText(String, CultureInfo, FlowDirection, Typeface, Double, Brush, NumberSubstitution, Double) FormattedText(String, CultureInfo, FlowDirection, Typeface, Double, Brush, NumberSubstitution, Double) FormattedText(String, CultureInfo, FlowDirection, Typeface, Double, Brush, NumberSubstitution, Double) FormattedText(String, CultureInfo, FlowDirection, Typeface, Double, Brush, NumberSubstitution, Double)

Initializes a new instance of the FormattedText class with the specified text, culture, flow direction, typeface, font size, foreground brush, number substitution behavior, and pixelsPerDip value.

FormattedText(String, CultureInfo, FlowDirection, Typeface, Double, Brush, NumberSubstitution, TextFormattingMode) FormattedText(String, CultureInfo, FlowDirection, Typeface, Double, Brush, NumberSubstitution, TextFormattingMode) FormattedText(String, CultureInfo, FlowDirection, Typeface, Double, Brush, NumberSubstitution, TextFormattingMode) 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.

FormattedText(String, CultureInfo, FlowDirection, Typeface, Double, Brush, NumberSubstitution, TextFormattingMode, Double) FormattedText(String, CultureInfo, FlowDirection, Typeface, Double, Brush, NumberSubstitution, TextFormattingMode, Double) FormattedText(String, CultureInfo, FlowDirection, Typeface, Double, Brush, NumberSubstitution, TextFormattingMode, Double) FormattedText(String, CultureInfo, FlowDirection, Typeface, Double, Brush, NumberSubstitution, TextFormattingMode, Double)

Initializes a new instance of the FormattedText class with the specified text, culture, flow direction, typeface, font size, foreground brush, number substitution behavior, and pixelsPerDip value.

Properties

Baseline Baseline Baseline Baseline

Gets the distance from the top of the first line to the baseline of the first line of a FormattedText object.

Extent Extent Extent Extent

Gets the distance from the topmost drawn pixel of the first line to the bottommost drawn pixel of the last line.

FlowDirection FlowDirection FlowDirection FlowDirection

Gets or sets the FlowDirection of a FormattedText object.

Height Height Height Height

Gets the distance from the top of the first line to the bottom of the last line of the FormattedText object.

LineHeight LineHeight LineHeight LineHeight

Gets the line height, or line spacing, between lines of text.

MaxLineCount MaxLineCount MaxLineCount MaxLineCount

Gets or sets the maximum number of lines to display. Text exceeding the MaxLineCount will not be displayed.

MaxTextHeight MaxTextHeight MaxTextHeight MaxTextHeight

Gets or sets the maximum height of a text column.

MaxTextWidth MaxTextWidth MaxTextWidth MaxTextWidth

Gets or sets the maximum text width (length) for a line of text.

MinWidth MinWidth MinWidth MinWidth

Gets the smallest possible text width that can fully contain the specified text content.

OverhangAfter OverhangAfter OverhangAfter OverhangAfter

Gets the distance from the bottom of the last line of text to the bottommost drawn pixel.

OverhangLeading OverhangLeading OverhangLeading OverhangLeading

Gets the maximum distance from the leading alignment point to the leading drawn pixel of a line.

OverhangTrailing OverhangTrailing OverhangTrailing OverhangTrailing

Gets the maximum distance from the trailing inked pixel to the trailing alignment point of a line.

PixelsPerDip PixelsPerDip PixelsPerDip PixelsPerDip

Get or sets the PixelsPerDip at which the text should be rendered.

Text Text Text Text

Gets the string of text to be displayed.

TextAlignment TextAlignment TextAlignment TextAlignment

Gets or sets the alignment of text within a FormattedText object.

Trimming Trimming Trimming Trimming

Gets or sets the means by which the omission of text is indicated.

Width Width Width Width

Gets the width between the leading and trailing alignment points of a line, excluding any trailing white-space characters.

WidthIncludingTrailingWhitespace WidthIncludingTrailingWhitespace WidthIncludingTrailingWhitespace WidthIncludingTrailingWhitespace

Gets the width between the leading and trailing alignment points of a line, including any trailing white-space characters.

Methods

BuildGeometry(Point) BuildGeometry(Point) BuildGeometry(Point) BuildGeometry(Point)

Returns a Geometry object that represents the formatted text, including all glyphs and text decorations.

BuildHighlightGeometry(Point) BuildHighlightGeometry(Point) BuildHighlightGeometry(Point) BuildHighlightGeometry(Point)

Returns a Geometry object that represents the highlight bounding box of the formatted text.

BuildHighlightGeometry(Point, Int32, Int32) BuildHighlightGeometry(Point, Int32, Int32) BuildHighlightGeometry(Point, Int32, Int32) BuildHighlightGeometry(Point, Int32, Int32)

Returns a Geometry object that represents the highlight bounding box for a specified substring of the formatted text.

Equals(Object) Equals(Object) Equals(Object) Equals(Object)

Determines whether the specified object is equal to the current object.

(Inherited from Object)
GetHashCode() GetHashCode() GetHashCode() GetHashCode()

Serves as the default hash function.

(Inherited from Object)
GetMaxTextWidths() GetMaxTextWidths() GetMaxTextWidths() GetMaxTextWidths()

Retrieves an array of text widths. Each element in the array represents the maximum text width of sequential lines of text.

GetType() GetType() GetType() GetType()

Gets the Type of the current instance.

(Inherited from Object)
MemberwiseClone() MemberwiseClone() MemberwiseClone() MemberwiseClone()

Creates a shallow copy of the current Object.

(Inherited from Object)
SetCulture(CultureInfo) SetCulture(CultureInfo) SetCulture(CultureInfo) SetCulture(CultureInfo)

Sets the CultureInfo for the entire set of characters in the FormattedText object.

SetCulture(CultureInfo, Int32, Int32) SetCulture(CultureInfo, Int32, Int32) SetCulture(CultureInfo, Int32, Int32) SetCulture(CultureInfo, Int32, Int32)

Sets the CultureInfo for a specified subset of characters in the FormattedText object.

SetFontFamily(FontFamily) SetFontFamily(FontFamily) SetFontFamily(FontFamily) SetFontFamily(FontFamily)

Sets the font family for a FormattedText object.

SetFontFamily(FontFamily, Int32, Int32) SetFontFamily(FontFamily, Int32, Int32) SetFontFamily(FontFamily, Int32, Int32) SetFontFamily(FontFamily, Int32, Int32)

Sets the font family for a specified subset of characters in the FormattedText object.

SetFontFamily(String) SetFontFamily(String) SetFontFamily(String) SetFontFamily(String)

Sets the font family for the entire set of characters in the FormattedText object.

SetFontFamily(String, Int32, Int32) SetFontFamily(String, Int32, Int32) SetFontFamily(String, Int32, Int32) SetFontFamily(String, Int32, Int32)

Sets the font family for a specified subset of characters in the FormattedText object.

SetFontSize(Double) SetFontSize(Double) SetFontSize(Double) SetFontSize(Double)

Sets the font size for the entire set of characters in the FormattedText object.

SetFontSize(Double, Int32, Int32) SetFontSize(Double, Int32, Int32) SetFontSize(Double, Int32, Int32) SetFontSize(Double, Int32, Int32)

Sets the font size for a specified subset of characters in the FormattedText object.

SetFontStretch(FontStretch) SetFontStretch(FontStretch) SetFontStretch(FontStretch) SetFontStretch(FontStretch)

Sets the font stretch value for the entire set of characters in the FormattedText object.

SetFontStretch(FontStretch, Int32, Int32) SetFontStretch(FontStretch, Int32, Int32) SetFontStretch(FontStretch, Int32, Int32) SetFontStretch(FontStretch, Int32, Int32)

Sets the font stretch value for a specified subset of characters in the FormattedText object.

SetFontStyle(FontStyle) SetFontStyle(FontStyle) SetFontStyle(FontStyle) SetFontStyle(FontStyle)

Sets the font style for the entire set of characters in the FormattedText object.

SetFontStyle(FontStyle, Int32, Int32) SetFontStyle(FontStyle, Int32, Int32) SetFontStyle(FontStyle, Int32, Int32) SetFontStyle(FontStyle, Int32, Int32)

Sets the font style for a specified subset of characters in the FormattedText object.

SetFontTypeface(Typeface) SetFontTypeface(Typeface) SetFontTypeface(Typeface) SetFontTypeface(Typeface)

Sets the font typeface for the entire set of characters in the FormattedText object.

SetFontTypeface(Typeface, Int32, Int32) SetFontTypeface(Typeface, Int32, Int32) SetFontTypeface(Typeface, Int32, Int32) SetFontTypeface(Typeface, Int32, Int32)

Sets the font typeface for a specified subset of characters in the FormattedText object.

SetFontWeight(FontWeight) SetFontWeight(FontWeight) SetFontWeight(FontWeight) SetFontWeight(FontWeight)

Sets the font weight for the entire set of characters in the FormattedText object.

SetFontWeight(FontWeight, Int32, Int32) SetFontWeight(FontWeight, Int32, Int32) SetFontWeight(FontWeight, Int32, Int32) SetFontWeight(FontWeight, Int32, Int32)

Changes the FontWeight for specified text within a FormattedText object.

SetForegroundBrush(Brush) SetForegroundBrush(Brush) SetForegroundBrush(Brush) SetForegroundBrush(Brush)

Changes the foreground Brush for an entire FormattedText object.

SetForegroundBrush(Brush, Int32, Int32) SetForegroundBrush(Brush, Int32, Int32) SetForegroundBrush(Brush, Int32, Int32) SetForegroundBrush(Brush, Int32, Int32)

Changes the foreground Brush for specified text within a FormattedText object.

SetMaxTextWidths(Double[]) SetMaxTextWidths(Double[]) SetMaxTextWidths(Double[]) SetMaxTextWidths(Double[])

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.

SetNumberSubstitution(NumberSubstitution) SetNumberSubstitution(NumberSubstitution) SetNumberSubstitution(NumberSubstitution) SetNumberSubstitution(NumberSubstitution)

Sets the number substitution behavior for the entire set of characters in the FormattedText object.

SetNumberSubstitution(NumberSubstitution, Int32, Int32) SetNumberSubstitution(NumberSubstitution, Int32, Int32) SetNumberSubstitution(NumberSubstitution, Int32, Int32) SetNumberSubstitution(NumberSubstitution, Int32, Int32)

Sets the number substitution behavior for specified text within a FormattedText object.

SetTextDecorations(TextDecorationCollection) SetTextDecorations(TextDecorationCollection) SetTextDecorations(TextDecorationCollection) SetTextDecorations(TextDecorationCollection)

Sets the TextDecorationCollection for the entire set of characters in the FormattedText object.

SetTextDecorations(TextDecorationCollection, Int32, Int32) SetTextDecorations(TextDecorationCollection, Int32, Int32) SetTextDecorations(TextDecorationCollection, Int32, Int32) SetTextDecorations(TextDecorationCollection, Int32, Int32)

Sets the TextDecorationCollection for specified text within a FormattedText object.

ToString() ToString() ToString() ToString()

Returns a string that represents the current object.

(Inherited from Object)

Applies to

See also