8.16. Formatting Existing Text
Problem
You want to format the existing text in a text field.
Solution
Pass a TextFormat object to
the TextField.setTextFormat(
) method.
Discussion
A text field allows minimal control over the formatting of the text
it displays, such as by setting the htmlText
property. Although you can set the color of the entire contents of
the text field using the textColor property, for
example, the TextField class
doesn’t offer precise control over character
formatting. However, the TextFormat class is a
“helper” class;
TextFormat objects can format the text displayed
in text fields.
The first step in formatting text is to create a
TextFormat object using the constructor method:
myTextFormat = new TextFormat( );
Next, assign values to the TextFormat
object’s properties as you desire:
myTextFormat.align = "center"; // Center-align the text. myTextFormat.bold = true; // Bold the text. myTextFormat.color = 0xFFFF00; // Make the text yellow. myTextFormat.blockIndent = 5; // Adjust the margin by five pixels.
You can apply text formatting to the existing text for an entire text
field by passing a TextFormat object to the text
field’s setTextFormat( )
method:
myTextField.setTextFormat(myTextFormat);
When you invoke the setTextFormat( ) method in
this way, the formatting from the TextFormat
object is applied to the text already assigned to the text field. The
formatting does not apply to any text assigned to the text field
after the setTextFormat( ) method is invoked. If additional ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access