Class

StyleRun


Description

Used to manage a style run within StyledText.

Property descriptions


StyleRun.Bold

Bold As Boolean

If True, applies the bold style to the control's caption and/or its text content if any.

Mac apps can only display font styles that are available. You cannot force a font to display in bold or italic if it does not have bold or italic variations available. In this situation, the Bold property will not affect the font.


StyleRun.FontName

FontName As String

Gets or sets the Font of the StyleRun.

This example gets the font of the first StyleRun.

MessageBox(TextArea1.StyledText.StyleRun(0).FontName)

This example sets the font of the first StyleRun.

TextArea1.StyledText.StyleRun(0).FontName = "Helvetica"

StyleRun.FontSize

FontSize As Integer

Gets or sets the font size of the StyleRun.

This example gets the font size of the first StyleRun.

TextField1.Text = TextArea1.StyledText.StyleRun(0).FontSize.ToString

This example sets the font size of the first StyleRun.

TextArea1.StyledText.StyleRun(0).FontSize = 16

StyleRun.Italic

Italic As Boolean

If True, applies the italic style to the control's caption and/or its text content if any.

Mac apps can only display font styles that are available. You cannot force a font to display in bold or italic if it does not have bold or italic variations available. In this situation, the Italic property will not affect the font.


StyleRun.Text

Text As String

This example gets the text of the first StyleRun.

TextArea2.Text = TextArea1.StyledText.StyleRun(0).Text

StyleRun.TextColor

TextColor As Color

Gets or sets the color of the caption or the text content. The default value is black.


StyleRun.Underline

Underline As Boolean

If True, applies the underline style to the control's caption and/or its text content if any.

Notes

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.

Sample code

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.Write(mb)
Next
out.Close ' close the file

Compatibility

All project types on all supported operating systems.

See also

Object parent class; Paragraph, Range, StyledText, TextArea classes.