From Xojo Documentation

You are currently browsing the old Xojo documentation site. Please visit the new Xojo documentation site!

Class (inherits from Object)

Used to manage a style run within StyledText.

Bold Italic Underline
FontName Text
FontSize TextColor


A StyleRun is a series of consecutive characters that have the same style attributes, as indicated by the StyleRun's properties. The Text property of a StyledText object is a series of StyleRuns.

You can get the total number of StyleRuns via the StyleRunCount method of the StyledText class and reference a StyleRun via the StyleRun method. You can get the starting and ending character positions (and, therefore, the length) of a StyleRun via the StyleRunRange method. The Range class gives you access to the StyleRun's start position, length, and end position.

If you are displaying the styled text in a TextArea, changes to a StyleRun's properties are not 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 update styled text in a TextArea is to make your changes using the StyledText methods instead of the StyleRun properties.


The following example saves styled text displayed in a TextArea as a series of StyleRuns that are appended to one another. The example illustrates manipulation of StyleRuns. However, the preferred way to save styled text is to use the RTFData property of the StyledText class. There is an example of a save as RTF in the examples section of the StyledText class.

Var out As BinaryStream
Var f As FolderItem
f = FolderItem.ShowSaveFileDialog(FileTypes1.Text, "Untitled.txt")
out = BinaryStream.Create(f, False)

// If we couldn't create the file, then bail out
If out = Nil Then Return

// Now we want to loop over all the StyleRuns
// and dump them out to the file.
Var mb As MemoryBlock
Var sr As StyleRun
Var i, count As Integer
Var textLen, fontLen As Integer
Var staticInfo As Integer

// We already know that a style run takes up a certain amount of space
// 3 bytes for the booleans, 2 for size and 4 for color
staticInfo = 1 + 1 + 1 + 2 + 4

// Get the number of styles
count = TextArea1.StyledText.StyleRunCount
For i = 0 To Count - 1 // get the StyleRuns
sr = TextArea1.StyledText.StyleRun(i)
textLen = sr.Text.Length + 1
fontLen = sr.FontName.Length + 1
// Stuff the style run content and style info into a memory block
mb = New MemoryBlock(staticInfo + fontLen + textLen)
mb.BooleanValue(0) = sr.Bold
mb.BooleanValue(1) = sr.Italic
mb.BooleanValue(2) = sr.Underline
mb.Short(3) = sr.Size
mb.ColorValue(5, 32) = sr.TextColor
mb.CString(9) = sr.Font
mb.CString(9 + fontLen) = sr.Text

// Now we can write that information to the file
out.Close // close the file

See Also

Paragraph, Range, StyledText, TextArea classes.