Table of Contents

TextField.setTextFormat( ) Method Flash 6

sets the formatting for a specified range of characters
theField.setTextFormat(textFormatObject)
theField.setTextFormat(beginIndex, textFormatObject)
theField.setTextFormat(beginIndex, endIndex, textFormatObject)

Arguments

textFormatObject

An instance of the TextFormat class.

beginIndex

The index of the first character whose formatting will be set by textFormatObject. Must be a nonnegative integer.

endIndex

The index following the last character whose formatting will be set by textFormatObject. Must be a nonnegative integer.

Description

The setTextFormat( ) method formats the span of characters from beginIndex to endIndex according to the properties of textFormatObject. Any null or undefined properties of textFormatObject have no effect on the target characters. The setTextFormat( ) method is invoked with one, two, or three arguments. When setTextFormat( ) is invoked with two arguments, the format applies to the single character at beginIndex. For example, here we apply a bold format to the first character of a text field:

// Create the text field
this.createTextField("theField_txt", 1, 0, 0, 150, 40);
// Assign the field some text for display
theField_txt.text = "Products and Services";
// Create a TextFormat object that specifies bold text
boldFormat = new TextFormat();
boldFormat.bold = true;
// Apply the format to the letter 'P', character 0
theField_txt.setTextFormat(0, boldFormat);

When setTextFormat( ) is invoked with three arguments, the format applies to the span of characters from beginIndex to endIndex - 1. For example, this code italicizes the word "and":

// Create a TextFormat object that specifies italic text
italicFormat = new TextFormat();
italicFormat.italic = true;
// Apply the format to the word 'and', characters 9 to 11 (inclusive)
theField_txt.setTextFormat(9, 12, italicFormat);

When setTextFormat( ) is invoked with one argument, the format applies to the all the characters in the field. For example, the following sets the font for the entire text of theField_txt to Arial:

// Create a TextFormat object that specifies the font "Arial"
arialFormat = new TextFormat();
arialFormat.font = "Arial";
// Apply the format to the entire text
theField_txt.setTextFormat(arialFormat);

Notice that the sequential formatting is not destructive; formatting changes are applied only for properties that are set, so the letter "P" remains bolded and the word "and" remains italicized.

Setting the value of a field's text property destroys all formatting for the field. For example, if we now add the word "Overview" to theField_txt.text, all characters are rerendered in the new text format for the field:

// Resets theField_txt's formatting
theField_txt.text += "Overview";

To add content to a text field without affecting its formatting, use the replaceSel( ) method.

Don't confuse setNewTextFormat( ) with setTextFormat( ); the former sets the format used for text that will be added to theField in the future, while the latter sets the format for characters already in theField. Both methods are valid, but the order in which they are used is crucial. Use setTextFormat( ) after setting the text property; use setNewTextFormat( ) before setting the text property.

Here, using setTextFormat( ), we first assign our text and then apply formatting to it:

// Create the text field
this.createTextField("title_txt", 1, 0, 0, 150, 40);
// Assign the field some text for display
title_txt.text = "What's New?";
// Now create and apply our TextFormat object
title_txt.setTextFormat(new TextFormat("Arial", 14, 0xFF0000));

If we mistakenly apply our format before the text is assigned, nothing happens:

// Oops! Wrong order...
title_txt.setTextFormat(new TextFormat("Arial", 14, 0xFF0000));
title_txt.text = "What's New?";

Conversely, when using setNewTextFormat( ), we must be sure to apply our format before assigning text to the text field:

title_txt.setNewTextFormat(new TextFormat("Arial", 14, 0xFF0000));
title_txt.text = "What's New?";

The setNewTextFormat( ) method is convenient when text is likely to change, because it applies to all future assignments for the field:

// The text "Latest News" is still red, 14-point Arial.
title_txt.text = "Latest News";

For a detailed description of TextFormat objects, see the TextFormat class. Note that text can also be formatted with HTML, as described under TextField.html and TextField.htmlText.

Example

The following code adds to all TextField objects a rainbow( ) method that randomly colors a field's text:

// Here's the method
TextField.prototype.rainbow = function () {
  for (var i = 0; i < this.length; i++) {
    var randColor = Math.floor(Math.random() * 0xFFFFFF);
    this.setTextFormat(i, new TextFormat(null, null, randColor));
  }
}
// Now let's create a text field to try it on
this.createTextField("theField_txt", 1, 50, 50, 160, 20);
// Set some text
theField_txt.text = "Ooh, what pretty colors!";
// Try it out!
theField_txt.rainbow();

See Also

TextField.getNewTextFormat( ), TextField.getTextFormat( ), TextField.html, TextField.htmlText, TextField.replaceSel( ), TextField.setNewTextFormat( ), the TextFormat class


Table of Contents