Class
StyledText
Description
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.
Methods
Name |
Parameters |
Returns |
Shared |
---|---|---|---|
Run As StyleRun |
|||
Start As Integer, Length As Integer = 1, Assigns value As Boolean |
|||
Start As Integer, Length As Integer = 1, Assigns value As String |
|||
Start As Integer, Length As Integer = 1, Assigns value As Boolean |
|||
Index As Integer |
|||
Index As Integer, Assigns value As TextAlignments |
|||
Index As Integer |
|||
Start As Integer, Length As Integer = 1, Assigns value As Single |
|||
Index As Integer |
|||
Index As Integer |
|||
Start As Integer, Length As Integer = 1, Assigns value As Color |
|||
Start As Integer, Length As Integer = 1, Assigns value As Boolean |
Property descriptions
StyledText.RTFData
RTFData As String
The styled text managed by the StyledText object in RTF format.
This property supports cross-platform RTF generation and parsing. The RTF parser supports only the features found in the StyledText class.
This example saves as RTF using the code snippet after the method that creates the StyleRuns.
Var f As FolderItem = FolderItem.ShowSaveFileDialog(TextTypes.TextRtf, "TestSaveRTF") If f <> Nil Then Var s As TextOutputStream = TextOutputStream.Create(f) s.Write(TextArea1.StyledText.RTFData) s.Close End If
Method descriptions
StyledText.AddStyleRun
AddStyleRun(Run As StyleRun)
StyledText.AddStyleRunAt
AddStyleRunAt(index As Integer, run As StyleRun)
Adds the StyleRun passed at the insertion point designated by index.
You can get the total number of StyleRuns in Text from the StyleRunCount method.
This example appends a StyleRun into a TextArea and then inserts another one in an earlier position.
TextArea2.StyledText.AddStyleRun(TextArea1.StyledText.StyleRun(1)) TextArea2.StyledText.AddStyleRunAt(0, TextArea1.StyledText.Stylerun(0))
StyledText.Bold
Bold(Start As Integer, Length As Integer = 1) As Boolean
Gets the Bold style to the selected text in Text.
Start is the starting position in Text. Start starts at zero. The default value for Length is 1. Returns a Boolean.
Bold(Start As Integer, Length As Integer = 1, Assigns value As Boolean)
Sets the Bold style to the selected text in Text.
Start is the starting position in Text. Start starts at zero. The default value for Length is 1.
Bold some text in a TextArea:
TextArea1.StyledText.Text = "Hello, World!" TextArea1.StyledText.Bold(7, 5) = True ' "World" is now bold
StyledText.FontName
FontName(Start As Integer, Length As Integer = 1) As String
Gets the font for the selected text in Text.
Start is the starting position in Text (0-based). The default value for Length is 1. Returns a String.
Note
On macOS, if Start + Length is greater than the Length of the String, then an OutOfBoundsException is raised. On other platforms, the change will apply to the end of the string.
FontName(Start As Integer, Length As Integer = 1, Assigns value As String)
Sets the font for the selected text in Text.
Start is the starting position in Text (0-based). The default value for Length is 1.
Note
On macOS, if Start + Length is greater than the Length of the String, then an OutOfBoundsException is raised. On other platforms, the change will apply to the end of the string.
Change the font for some text in a TextArea:
TextArea1.StyledText.Text = "Hello, World!" TextArea1.StyledText.FontName(7, 5) = "Courier"
StyledText.Italic
Italic(Start As Integer, Length As Integer = 1) As Boolean
Gets the Italic style to the selected text in Text.
Start is the starting position in Text. The default value for Length is 1. Returns a Boolean.
Italic(Start As Integer, Length As Integer = 1, Assigns value As Boolean)
Sets the Italic style to the selected text in Text.
Start is the starting position in Text. Start starts at zero. The default value for Length is 1.
Italic some text in a TextArea:
TextArea1.StyledText.Text = "Hello, World!" TextArea1.StyledText.Italic(7, 5) = True ' "World" is now italic
StyledText.Paragraph
Paragraph(Index As Integer) As Paragraph
StyledText.ParagraphCount
ParagraphCount As Integer
Returns as an Integer the number of paragraphs in Text.
Completely empty paragraphs (e.g., blank lines) do not count as paragraphs.
StyledText.ParagraphTextAlignment
ParagraphTextAlignment(Index As Integer, Assigns value As TextAlignments)
Aligns the paragraph indicated by Index.
Assign an alignment to ParagraphTextAlignment using one of the TextAlignments values.
Important
Not supported with Linux TextFields.
StyledText.RemoveStyleRunAt
RemoveStyleRunAt(Index As Integer)
Removes the StyleRun indicated by Index.
You can get the total number of StyleRuns in Text from the StyleRunCount method.
This example removes StyleRun 3.
TextArea1.StyledText.RemoveStyleRunAt(3)
StyledText.Size
Size(Start As Integer, Length As Integer = 1) As Single
Gets the font size for the selected text in Text.
Start is the starting position in Text. The default value for Length is 1. Returns a Single.
Size(Start As Integer, Length As Integer = 1, Assigns value As Single)
Sets the font size for the selected text in Text.
Start is the starting position in Text. The default value for Length is 1.
Change size of text in a TextArea:
TextArea1.StyledText.Text = "Hello, World!" TextArea1.StyledText.Size(7, 5) = 32 ' "World" is now 32 point
StyledText.StyleRun
StyleRun(Index As Integer) As StyleRun
Returns a StyleRun specified by Index. Index is zero-based.
StyledText.StyleRunCount
StyleRunCount As Integer
Returns as an Integer the number of style runs in Text.
Use this in conjunction with the StyleRun and StyleRunRange methods to get information about each style run.
StyledText.StyleRunRange
StyleRunRange(Index As Integer) As Range
Returns a Range for the specified StyleRun. Index is zero-based.
Properties of the Range class give you access to the StyleRun's starting and ending positions, and length.
The following example loops through the StyleRuns in a block of StyledText that is displayed in a TextArea. It uses the StartPos and Length properties of the Range class to get the position of each StyleRun and displays it and the text of each StyleRun in a ListBox.
Var count As Integer = TextArea1.StyledText.StyleRunCount ' get the number of StyleRuns For i As Integer = 0 To count - 1 ' loop through them ListBox1.AddRow(TextArea1.StyledText.StyleRunRange(i).StartPos.ToString) ListBox1.CellTextAt(i, 1) = TextArea1.StyledText.StyleRunRange(i).Length.ToString ListBox1.CellTextAt(i, 2) = TextArea1.StyledText.StyleRun(i).Text Next
StyledText.Text
Text As String
The text whose style attributes are managed via the instance of this class.
The Start and Index parameters of the methods of this class are zero-based and referenced from the start of Text. Set the value of Text using the assignment operator.
Setting the Text property clears any previously set style information. If you want to modify text inline without changing the style you will need to use the StyleRuns, adding and removing them as necessary.
Add some styled text to a TextArea:
TextArea1.StyledText.Text = "Hello, World!" TextArea1.StyledText.Bold(7, 5) = True ' "World" is now bold
StyledText.TextColor
TextColor(Start As Integer, Length As Integer = 1) As Color
Gets or sets the text color for the selected text in Text.
Start is the starting position in Text. The default value for Length is 1. Returns a Color.
TextColor(Start As Integer, Length As Integer = 1, Assigns value As Color)
Sets the text color for the selected text in Text.
Start is the starting position in Text. The default value for Length is 1.
Bold some text in a TextArea:
TextArea1.StyledText.Text = "Hello, World!" TextArea1.StyledText.TextColor(7, 5) = &cff0000 ' "World" is now red
StyledText.Underline
Underline(Start As Integer, Length As Integer = 1) As Boolean
Gets the Underline style to the selected text in Text.
Start is the starting position in Text. The default value for Length is 1. Returns a Boolean.
Underline(Start As Integer, Length As Integer = 1, Assigns value As Boolean)
Sets the Underline style to the selected text in Text.
Start is the starting position in Text. The default value for Length is 1.
Underline some text in a TextArea:
TextArea1.StyledText.Text = "Hello, World!" TextArea1.StyledText.Underline(7, 5) = True ' "World" is now underline
Notes
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).
Sample code
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:
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)
s.Write(TextArea1.StyledText.RTFData)
End If
Compatibility
Project Types |
Console, Desktop, Web |
Operating Systems |
All |
See also
Object parent class; Paragraph, Range, StyleRun, DesktopTextArea classes.