TextField.setNewTextFormat( ) Method

ActionScript for Flash MX: The Definitive Guide, 2nd Edition
By Colin Moock
Chapter 18.  ActionScript Language Reference
TextField.setNewTextFormat( ) Method Flash 6

sets the default formatting for the text field
theField.setNewTextFormat(textFormatObject)

Arguments

textFormatObject

An instance of the TextFormat class.

Description

The setNewTextFormat( ) method sets the default formatting for theField. After it is invoked, any text added to theField via TextField.text or TextField.replaceSel( ), or by the user, is formatted according to the property values of textFormatObject. However, setNewTextFormat( ) has no effect on characters already in theField. To reformat characters already in theField, use setTextFormat( ).

The setNewTextFormat( ) method is perfect for establishing the default formatting of a text field whose content is likely to change. For example, suppose we want to display a player's score in blue, 10-point Courier New. Each time the player earns points, the score changes and we update our score text field. To ensure that the formatting of the text field is retained for each update, we use setNewTextFormat( ) immediately after the field is created:

// Create the text field. this.createTextField("score_txt", 1, 50, 50, 100, 20); // Assign the default TextFormat. Make sure this comes before any text is assigned! score_txt.setNewTextFormat(new TextFormat("Courier New", 10, 0x0000FF)); // Set score output. score_txt.text = "0000"; // Change score output. Formatting is retained. score_txt.text = "0020";

To examine the existing default format for theField, use getNewTextFormat( ). 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.

Usage

When the insertion point is placed in a text field by the user or with Selection.setSelection( ), the field's default text format is set to match the character to the insertion point's right (or left, in the case of the last character). To prevent any subsequent user input from picking up the formatting of existing text, we must manually restore the default text format with an interval, as shown in Example 18-8. The workaround, however, is not perfect on slower machines, the interval cannot fire fast enough to intercept every keystroke and the occasional character takes on the formatting of existing text.

Example 18-8. Resetting new text format after selection
// Create the text field and set it to accept user input this.createTextField("theField_txt", 1, 0, 0, 150, 40); theField_txt.type = "input"; // Store the original new text format in a property theField_txt.newTextFormat = theField_txt.getNewTextFormat(); // Define a callback to handle setFocus events theField_txt.onSetFocus = function (oldFocus, newFocus) {   // If the field is focused, start an interval that   // continuously resets the original newTextFormat   if (newFocus =  = this) {     this.restoreInterval = setInterval(this, "restoreNewTextFormat", 1);   } else if (oldFocus =  = this) {     // Stop resetting if the field is unfocused     clearInterval(this.restoreInterval);   } } // Create the interval function to reset the newTextFormat theField_txt.restoreNewTextFormat = function () {   this.setNewTextFormat(this.newTextFormat); } // Register the text field to receive setFocus events Selection.addListener(theField_txt); // Assign the field some text for display theField_txt.text = "What time is it?" // Create a TextFormat object with a font property of "Arial" sansFormat = new TextFormat("Arial"); // Apply the format to the word 'What', characters 0 to 3 (inclusive) theField_txt.setTextFormat(0, 4, sansFormat);

When text is inserted with the replaceSel( ) method, it's easy to force the appropriate new text format simply invoke setNewTextFormat( ) after the call to Selection.setSelection( ), as shown in Example 18-9.

Example 18-9. Setting the new text format before replacing a selection
// Focus the field we're going to update Selection.setFocus(theField_txt); // Position the insertion point at the first character Selection.setSelection(1,1); // *Now* set the newTextFormat theField_txt.setNewTextFormat(theField_txt.newTextFormat); // Proceed with replaceSel() call theField_txt.replaceSel("this is the new text");

See Also

The Selection class, TextField.getTextFormat( ), TextField.getNewTextFormat( ), TextField.replaceSel( ), TextField.setTextFormat( ), the TextFormat class



    ActionScript for Flash MX. The Definitive Guide
    ActionScript for Flash MX: The Definitive Guide, Second Edition
    ISBN: 059600396X
    EAN: 2147483647
    Year: 2002
    Pages: 780
    Authors: Colin Moock

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net