Class

# StyleRun

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

## Description

Used to manage a style run within `StyledText</api/text/styledtext>`.

## Properties

<div class="rst-class">

table-centered_columns_3_and_4

</div>

| Name                            | Type                               | Read-Only | Shared |
|---------------------------------|------------------------------------|-----------|--------|
| `Bold<stylerun.bold>`           | `Boolean</api/data_types/boolean>` |           |        |
| `FontName<stylerun.fontname>`   | `String</api/data_types/string>`   |           |        |
| `FontSize<stylerun.fontsize>`   | `Single</api/data_types/single>`   |           |        |
| `Italic<stylerun.italic>`       | `Boolean</api/data_types/boolean>` |           |        |
| `Text<stylerun.text>`           | `String</api/data_types/string>`   |           |        |
| `TextColor<stylerun.textcolor>` | `Color</api/data_types/color>`     |           |        |
| `Underline<stylerun.underline>` | `Boolean</api/data_types/boolean>` |           |        |

## Property descriptions

<div id="stylerun.bold">

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

</div>

<div class="rst-class">

forsearch

</div>

StyleRun.Bold

**Bold** As `Boolean</api/data_types/boolean>`

> If `True</api/language/true>`, applies the bold style to the control's caption and/or its text content if any.
>
> macOS 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.

<div id="stylerun.fontname">

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

</div>

<div class="rst-class">

forsearch

</div>

StyleRun.FontName

**FontName** As `String</api/data_types/string>`

> Gets or sets the Font of the <span class="title-ref">StyleRun</span>.
>
> This example gets the font of the first <span class="title-ref">StyleRun</span>.
>
> ``` xojo
> MessageBox(TextArea1.StyledText.StyleRun(0).FontName)
> ```
>
> This example sets the font of the first <span class="title-ref">StyleRun</span>.
>
> ``` xojo
> TextArea1.StyledText.StyleRun(0).FontName = "Helvetica"
> ```

<div id="stylerun.fontsize">

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

</div>

<div class="rst-class">

forsearch

</div>

StyleRun.FontSize

**FontSize** As `Single</api/data_types/single>`

> Gets or sets the font size of the <span class="title-ref">StyleRun</span>.
>
> This example gets the font size of the first <span class="title-ref">StyleRun</span>.
>
> ``` xojo
> TextField1.Text = TextArea1.StyledText.StyleRun(0).FontSize.ToString
> ```
>
> This example sets the font size of the first <span class="title-ref">StyleRun</span>.
>
> ``` xojo
> TextArea1.StyledText.StyleRun(0).FontSize = 16
> ```

<div id="stylerun.italic">

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

</div>

<div class="rst-class">

forsearch

</div>

StyleRun.Italic

**Italic** As `Boolean</api/data_types/boolean>`

> If `True</api/language/true>`, applies the italic style to the control's caption and/or its text content if any.
>
> macOS 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.

<div id="stylerun.text">

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

</div>

<div class="rst-class">

forsearch

</div>

StyleRun.Text

**Text** As `String</api/data_types/string>`

> This example gets the text of the first <span class="title-ref">StyleRun</span>.
>
> ``` xojo
> TextArea2.Text = TextArea1.StyledText.StyleRun(0).Text
> ```

<div id="stylerun.textcolor">

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

</div>

<div class="rst-class">

forsearch

</div>

StyleRun.TextColor

**TextColor** As `Color</api/data_types/color>`

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

<div id="stylerun.underline">

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

</div>

<div class="rst-class">

forsearch

</div>

StyleRun.Underline

**Underline** As `Boolean</api/data_types/boolean>`

> If `True</api/language/true>`, applies the underline style to the control's caption and/or its text content if any.

## Notes

A <span class="title-ref">StyleRun</span> is a series of consecutive characters that have the same style attributes, as indicated by the <span class="title-ref">StyleRun</span>'s properties. The Text property of a `StyledText</api/text/styledtext>` object is a series of StyleRuns.

You can get the total number of StyleRuns via the StyleRunCount method of the `StyledText</api/text/styledtext>` class and reference a <span class="title-ref">StyleRun</span> via the <span class="title-ref">StyleRun</span> method. You can get the starting and ending character positions (and, therefore, the length) of a <span class="title-ref">StyleRun</span> via the StyleRunRange method. The `Range</api/text/range>` class gives you access to the <span class="title-ref">StyleRun</span>'s start position, length, and end position.

If you are displaying the styled text in a `TextArea</api/user_interface/desktop/desktoptextarea>`, changes to a <span class="title-ref">StyleRun</span>'s properties are not reflected in the `TextArea</api/user_interface/desktop/desktoptextarea>`. If you want to operate on a <span class="title-ref">StyleRun</span>, 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</api/user_interface/desktop/desktoptextarea>` is to make your changes using the `StyledText</api/text/styledtext>` methods instead of the <span class="title-ref">StyleRun</span> properties.

## Sample code

The following example saves styled text displayed in a `TextArea</api/user_interface/desktop/desktoptextarea>` 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</api/text/styledtext>` class. There is an example of a save as RTF in the examples section of the `StyledText</api/text/styledtext>` class.

``` xojo
Var f As FolderItem = FolderItem.ShowSaveFileDialog(FileTypes1.Text, "Untitled.txt")
Var out As BinaryStream = 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.Int16Value(3) = sr.FontSize
  mb.ColorValue(5, 32) = sr.TextColor
  mb.CString(9) = sr.FontName
  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

|                       |                       |
|-----------------------|-----------------------|
| **Project Types**     | Console, Desktop, Web |
| **Operating Systems** | All                   |

<div class="seealso">

`Object</api/data_types/additional_types/object>` parent class; `Paragraph</api/text/paragraph>`, `Range</api/text/range>`, `StyledText</api/text/styledtext>`, `TextArea</api/user_interface/desktop/desktoptextarea>` classes.

</div>
