MaxTextWidth Property

FormattedText.MaxTextWidth Property

 

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

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

public double MaxTextWidth { get; set; }

Property Value

Type: System.Double

The maximum text width for a line of text, provided in device-independent units (1/96th inch per unit).

Text will wrap to avoid exceeding the MaxTextWidth limit.

Glyph alignment edges must be kept within the line count limits. Because many fonts contain glyphs that slightly overhang their alignment edges, allow at least 1/6 em (font size) margin beyond the line count requested, to leave room for such overhangs.

A zero value for line count is treated as an infinite line count.

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

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
Available since 3.0
Return to top
Show:
© 2016 Microsoft