Skip to main content
Row

RichTextString.Characters() Method

Returns a range of all characters in the current rich text string.

Namespace: DevExpress.Spreadsheet

Assembly: DevExpress.Spreadsheet.v24.1.Core.dll

NuGet Package: DevExpress.Spreadsheet.Core

Declaration

public RichTextRange Characters()

Returns

Type Description
RichTextRange

A RichTextRange object that contains the entire rich text string.

Remarks

Use the Characters method to format specific characters within the cell text. This method returns the RichTextRange object that represents the text subset and allows you to set its font characteristics using the RichTextRange.Font property or RichTextRange.SetFont method. To assign the rich formatted text to a cell, call the CellRange.SetRichText method.

The example below shows how to use the Characters method overloads to format the first word in a cell and preserve the cell formatting for the remaining cell text.

// Access a cell in a worksheet.
Cell cell = worksheet.Cells["B2"];

// Set the cell text and specify its font settings. 
cell.Value = "Rich text formatting";
cell.Font.Name = "Arial";
cell.Font.Size = 14;

// Get the RichTextString object containing the cell text. 
RichTextString richText = cell.GetRichText();

// Obtain the current cell font and apply it to all characters within the cell text.
richText.Characters().SetFont(cell.Font);

// Change formatting of the first word.  
richText.Characters(0, 4).Font.Bold = true;

// Assign the rich formatted text to the cell. 
cell.SetRichText(richText);

When you use the Characters method to apply rich formatting to a region of cell text, a corresponding RichTextRun object is created and added to the RichTextString.Runs collection. However, if the font settings you specify for text characters coincide with the previous or next run of the cell’s rich text, these runs are combined into a single text run and a new item is not created.

To view font characteristics applied to a portion of cell text, use the RichTextRange.Font property. This property returns the SpreadsheetFontProperties object that contains font attributes of the specified text range. If this text range includes multiple text runs, the SpreadsheetFontProperties property values are null for differing font characteristics.

Refer to the How to: Apply Rich Formatting to Cell Text document for more examples on how to apply rich formatting to the cell content.

See Also