From Xojo Documentation
Jump to: navigation, search

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.


AppendStyleRun ParagraphAlignment StyleRunRange
Bold ParagraphCount Text
Font RemoveStyleRun TextColor
InsertStyleRun Size Underline
Italic StyleRun
Paragraph StyleRunCount

Class Constants

The following class constants can be used to specify the alignment of the styled text with the ParagraphAlignment method.

Class Constant Description
AlignDefault Default alignment.
AlignLeft Left alignment.
AlignCenter Center alignment
AlignRight Right alignment


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. If you want to operate on a StyleRun, you must remove the old run and replace it with the new run. Of course, the easier way to do it would be to make your 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). Not all of the style attributes for selections in the TextArea class are available in the StyleRun class, such as Outline, Condense, Shadow, and Extend. These were supported only on Mac OS "classic".


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.

Dim text As String // text to be displayed in TextArea
Dim st, ln As Integer // start and length values of a paragraph
//define four paragraphs in Text
text = "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 = text // 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, Len(text) ) = "Comic Sans MS"

// center align second paragraph
TextArea1.StyledText.ParagraphAlignment(1) = Paragraph.AlignCenter
// set this paragraph in Helvetica, 18 pt bold, red
// first get the start and length values for this paragraph...
st = TextArea1.StyledText.Paragraph(1).StartPos
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:


The following code, appended 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.

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

See Also

Paragraph, Range, StyleRun, TextArea classes.

Personal tools

Starting Out
Dig Deeper
More Help