This topic has not yet been rated - Rate this topic

TextSource.GetTextRun Method

Retrieves a TextRun starting at a specified TextSource position.

Namespace:  System.Windows.Media.TextFormatting
Assembly:  PresentationCore (in PresentationCore.dll)
public abstract TextRun GetTextRun(
	int textSourceCharacterIndex
)

Parameters

textSourceCharacterIndex
Type: System.Int32

Specifies the character index position in the TextSource where the TextRun is retrieved.

Return Value

Type: System.Windows.Media.TextFormatting.TextRun
A value that represents a TextRun, or an object derived from TextRun.

A text run is a sequence of characters sharing a single property set. Any change to the format, such as font family, font style, foreground color, text decoration, or any other formatting effect, breaks the text run. The TextRun class is the root of a type hierarchy representing several types of text content processed by TextFormatter. Each class that is derived from TextRun represents a distinct type of text content.

Class

Description

TextRun

Root of the hierarchy. Defines a group of characters that share the same set of character properties.

TextCharacters

Defines a collection of character glyphs from a distinct physical typeface.

TextEmbeddedObject

Defines a type of text content in which measuring, hit testing, and drawing of the entire content is done as a distinct entity. An example of this type of content is a button in the middle of the line of text.

TextEndOfLine

Defines a line-break character code.

TextEndOfParagraph

Defines a paragraph-break character code. Derives from TextEndOfLine.

TextEndOfSegment

Defines a segment break marker.

TextHidden

Defines a range of non-visible characters.

TextModifier

Defines the beginning of a modification scope.

In the following example, an override for the GetTextRun method is implemented.

// Retrieve the next formatted text run for the text source. 
public override TextRun GetTextRun(int textSourceCharacterIndex)
{
    // Determine whether the text source index is in bounds. 
    if (textSourceCharacterIndex < 0)
    {
        throw new ArgumentOutOfRangeException("textSourceCharacterIndex", "Value must be greater than 0.");
    }

    // Determine whether the text source index has exceeded or equaled the text source length. 
    if (textSourceCharacterIndex >= _text.Length)
    {
        // Return an end-of-paragraph indicator -- a TextEndOfParagraph object is a special type of text run. 
        return new TextEndOfParagraph(1);
    }

    // Create and return a TextCharacters object, which is formatted according to 
    // the current layout and rendering properties. 
    if (textSourceCharacterIndex < _text.Length)
    {
        // The TextCharacters object is a special type of text run that contains formatted text. 
        return new TextCharacters(
           _text,                                       // The text store
           textSourceCharacterIndex,                    // The text store index
           _text.Length - textSourceCharacterIndex,     // The text store length 
           new CustomTextRunProperties());              // The layout and rendering properties
    }

    // Return an end-of-paragraph indicator if there is no more text source. 
    return new TextEndOfParagraph(1);
}

.NET Framework

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

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows Phone 8.1, Windows Phone 8, Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.