You want to add formatting to some, but not all, text in a text field, or you want to apply different formatting to various parts of a text field.
Create a TextFormat object and use it to format a substring of the text field using one of the setTextFormat( ) method variations.
You can format an entire text field as shown in Recipe 8.16, or you can use one of the versions of the setTextFormat( ) method to format just a portion of a text field. These variations allow you to apply formatting to the specified character range only.
You can set the formatting for a single character within a text field by invoking the setTextFormat( ) method and passing it two parameters:
The zero-relative index of the character to which the formatting should be applied
A reference to a TextFormat object
This example applies the formatting to the first character only:
myTextField.setTextFormat(0, myTextFormat);
Alternatively, if you want to apply the formatting to a range of characters, you can invoke setTextFormat( ) with three parameters:
The beginning, zero-relative character index
The index of the character after the last character in the desired range
A reference to a TextFormat object
This example applies the formatting to the 1st through 10th characters:
myTextField.setTextFormat(0, 10, myTextFormat);
You may notice that certain formatting options are not applied under certain circumstances when you try to format portions of a text field. For example, the text alignment will be applied only if the formatting is applied to the first character in a line.
Recipe 8.16