From Xojo Documentation

You are currently browsing the old Xojo documentation site. It will go offline as of October 2, 2023. Please visit the new Xojo documentation site! - you will be redirected shortly...

Class (inherits from Object)

Used to managed styled text independently of its display in a TextArea. For a TextArea, you can access the properties and methods of this class via the StyledText property of that class.

AddStyleRun ParagraphCount StyleRunRange
AddStyleRunAt ParagraphTextAlignment Text
Bold RemoveStyleRunAt TextColor
FontName Size Underline
Italic StyleRun
Paragraph StyleRunCount


The StyledText class enables you to apply style attributes to the StyledText class. This means that obtaining a StyleRun (using StyleRun()) and setting the style attributes for that run will not be reflected in the TextArea. To operate on a StyleRun, you must remove the old run and replace it with the new run. The preferred way to do it would be to make changes using the StyledText methods instead of the StyleRun properties.

Because the TextArea already has selection style attributes, the StyledText class honors that information. This means that you can set the style information using the selection methods that are available within the TextArea class, and they will be reflected when getting StyleRun information (and vice versa).


The following example uses the methods of the StyledText class to mark up and align text and display it in a TextArea. In order for the styled text to display, you must turn on the Multiline and Styled properties of the TextArea (both are on by default).

Var txt As String // text to be displayed in TextArea
Var st, ln As Integer // start and length values of a paragraph
// define four paragraphs in Text
txt = "This is the text that we are going to save " _
+ "into our file from the TextArea." + EndOfLine _
+ "Isn't that interesting?" + EndOfLine _
+ "Man, I sure do love using Xojo to take care of my projects."

TextArea1.StyledText.Text = txt // four paragraphs in Text
TextArea1.StyledText.Bold(5, 2) = True
TextArea1.StyledText.TextColor(5, 2) = &cFF0000 // bold and red
TextArea1.StyledText.Size(7, 10) = 16
TextArea1.StyledText.Underline(12, 4) = True // 16 pt underline
TextArea1.StyledText.Size(100, 4) = 18
TextArea1.StyledText.TextColor(100, 4) = &cFF00FF // 18 pt and Magenta
TextArea1.StyledText.Font(0, txt.Length) = "Comic Sans MS"

// center align second paragraph
TextArea1.StyledText.ParagraphTextAlignment(1) = TextAlignments.Center

// set this paragraph in Helvetica, 18 pt bold, red
// first get the start and length values for this paragraph...
st = TextArea1.StyledText.Paragraph(1).StartPosition
ln = TextArea1.StyledText.Paragraph(1).Length + 1

// next apply attributes...
TextArea1.StyledText.Bold(st, ln) = True
TextArea1.StyledText.Font(st, ln) = "Helvetica"
TextArea1.StyledText.Size(st, ln) = 18
TextArea1.StyledText.TextColor(st, ln) = &cFF0000

The resulting text area looks like this:

Styled Text Output.png

The following code, added to the end of the previous method, saves the styled text to disk in RTF format. It assumes that the File Type Set, "TextTypes" has one item, TextRTF, that defines the RTF file type.

Var f As FolderItem = FolderItem.ShowSaveFileDialog(TextTypes.TextRtf, "TestSaveRTF")
If f <> Nil Then
Var s As TextOutputStream = TextOutputStream.Create(f)
End If

See Also

Paragraph, Range, StyleRun, TextArea classes.