Skip to main content

TextSpan Class

A span of document content.

Namespace: DevExpress.Blazor.RichEdit

Assembly: DevExpress.Blazor.RichEdit.v24.1.dll

NuGet Package: DevExpress.Blazor.RichEdit

Declaration

public class TextSpan :
    DocumentElementBase,
    ICharacterProperties,
    ICharacterProperties

Remarks

A TextSpan object contains the following information:

  • The textual representation of the span content (Text).
  • The span interval in the sub-document (Interval).
  • Style and format settings of characters in the span (for instance, FontSize, Underline).

Call a GetTextSpanAsync method overload to get a span contained in the specified sub-document interval. Use this span to change format settings of characters in the span.

Format Text

Pass a CharacterProperties object to the ChangePropertiesAsync method to change character properties of the text span. You can specify character properties manually or use the CopyFrom(TextSpan) method to copy properties from another text span.

The following code snippet applies bold formatting to characters in the selected intervals.

<DxRichEdit @ref="richEdit" @bind-Selection="@selection" />
@code {
    DxRichEdit richEdit { get; set; }
    Selection selection { get; set; }
    @* ... *@
        /* Surround the code that contains an asynchronous operation with a try-catch block to handle
        the OperationCanceledException. This exception is thrown when an asynchronous operation is cancelled. */
        try {
            IReadOnlyList<Interval> intervals = richEdit.Selection.Intervals;
            foreach (Interval interval in intervals) {
                TextSpan boldTextSpan = await richEdit.DocumentAPI.GetTextSpanAsync(interval);
                await boldTextSpan.ChangePropertiesAsync(properties => {
                    properties.FontBold = true;
                });
            }
        }
        catch (OperationCanceledException e) {
            Console.WriteLine($"{nameof(OperationCanceledException)} thrown with message: {e.Message}");
        }
}

Run Demo: RichEdit – Document API

Clear Formatting

The ChangeDefaultCharacterPropertiesAsync method allows you to change the document’s default character formatting. To reset character formatting in a sub-document or its interval to the default settings, call the sub-document’s ClearFormattingAsync method overload.

The following code snippet configures the default character formatting and applies it to a document once the document is loaded in the Rich Text Editor:

<DxRichEdit DocumentLoaded=OnDocumentLoaded/>

@code {
    async Task OnDocumentLoaded(Document document) {
        await document.ChangeDefaultCharacterPropertiesAsync(properties => {
            properties.FontName = "Times New Roman";
            properties.FontSize = 12;
        });
        await document.ClearFormattingAsync();
    }
}

The Font Dialog

The Rich Text Editor invokes this dialog when a user selects the Font… context menu command. The Font dialog allows users to change font formatting of the selected characters.

Font Dialog

Inheritance

See Also