TextField Class | Flash 6 |
display and manipulate text on screen |
None. TextField objects are created manually in the authoring tool or programmatically via the MovieClip.createTextField( ) method.
Opacity percentage: 0 is transparent; 100 is opaque.
Matches the text field's bounding rectangle size to its text.
Boolean; turns text field background on and off.
RGB number specifying the text field's background color.
Boolean; turns text field border on and off.
RGB number specifying the text field's border color.
The 1-relative index of the text field's lowest visible line.
Boolean; enables or disables condensing of whitespace in HTML text.
Boolean; renders text using fonts exported with the movie.
Height of the text field's bounding box, in pixels.
Text's horizontal scroll position, in pixels.
Boolean; enables or disables HTML display.
HTML source code to render in the text field.
The number of characters in the text field.
Limits the allowed length of user input.
The farthest position text can be scrolled to the left.
The last legal top line of a text field.
Boolean; enables or disables multiple-line user input.
The instance name of the TextField object.
A reference to the movie clip in which the text field resides.
Boolean; obscures characters displayed in the field.
Limits text entry to a series of characters.
Rotation, in degrees, of the text field.
The current top line displayed in the text field.
Boolean; enables or disables selection by the user.
Boolean; includes or excludes the text field from the current tab order.
Specifies the text field's index in the custom tab order.
The target path of the text field, in slash syntax.
Specifies the characters to be rendered in the field.
Sets the color for all characters in the text field.
Pixel height of all the text in the text field.
Pixel width of the longest line in the text field.
Specifies whether the field can accept user input.
The network address of the movie clip that contains the field.
Associates a variable with the text property.
Boolean; whether the text field is shown or hidden.
Width of the text field's bounding box, in pixels.
Boolean; enables or disables automatic line-breaking for long lines.
Horizontal location of the text field, in pixels.
Horizontal location of mouse pointer, relative to the text field.
Width of the text field, as a percentage of original.
Vertical location of the text field, in pixels.
Vertical location of mouse pointer, relative to the text field.
Height of the text field, as a percentage of original.
Returns an array of fonts on the user's system.
Registers an object to receive onChanged( ) and onScroller( ) events.
Returns the position of the text field in the movie stack.
Retrieves the default TextFormat object for the text field.
Retrieves a TextFormat object for specified characters.
Cancels event notices for the specified listener.
Deletes a runtime-created text field.
Replaces selected text (without disturbing formatting).
Sets the default formatting for the text field.
Sets the formatting of specified characters.
Callback invoked when user input is detected.
Callback invoked when the field loses focus.
Callback invoked when any scroll property changes.
Callback invoked when the field gains focus.
Occurs when user input is detected.
Occurs when any scroll property changes.
The TextField class provides control over text displayed on screen. Each TextField object represents a rectangular text container that can be filled with text, sized, or formatted, and can even receive user input. When we speak of a "text field," we mean the rectangular container on screen; when we speak of a TextField object or instance, we mean the ActionScript object that controls a text field.
TextField objects are not constructed with the new operator; instead, they are created automatically by Flash whenever author-time text is drawn (with the Text tool) or when the MovieClip.createTextField( ) method is invoked. For example, the following code creates a text field named theField_txt, and places it on depth 1, at Stage coordinates (0,0), with a width of 200 and a height of 20. The this keyword refers to the current movie clip. The suffix "_txt" activates code hinting in the Actions panel:
this.createTextField("theField_txt", 1, 0, 0, 200, 20);
In order to gain access to a TextField instance for a field created at authoring time, we must set its type to Dynamic Text or Input Text and assign it an instance name via the Property inspector.
The following steps describe the typical workflow for creating an ActionScript-controllable text field at authoring time:
Select the Text tool.
Drag a rectangle big enough to display the desired text on stage .
Make sure the Property inspector (Window Properties) is open.
In the Property inspector, for Type, select Dynamic Text or Input Text (the default type, Static Text, cannot be accessed through ActionScript).
In the Property inspector, for Instance Name, type someName_txt. An Instance Name cannot be specified until the Type is set, as described in Step 4.
For instructions on deleting a text field at runtime, see the TextField.removeTextField( ) method.
Plain text displayed in a text field is retrieved through the text property and set through either the replaceSel( ) method or the text property. HTML text can be set through the htmlText property, provided that the html property is also set.
|
Hence, in Flash 5, we used the following syntax to set the highScore_txt text field to "15, 000":
highScore_txt = "15, 000";
But in Flash 6, we must use the text property to set a text field's text:
highScore_txt.text = "15, 000";
The Var: text box in Flash MX's text field Property inspector is retained for backward compatibility, as is the TextField.variable property. When authoring for Flash Player 6, assign all text fields an instance name and use the text property to access their content.
By default, Dynamic and Input text fields are selectable by the user. To create a nonselectable text field for display only, set its selectable property to false:
output_txt.selectable = false;
When a user selects a portion of a dynamic or user-input text field, the indexes of the selected characters are stored in the Selection object. Using the Selection object, we can check which part of a text field a user has selected; we can even select a part of a text field programmatically. The Selection object can also give keyboard focus to a particular text field or tell us which text field is currently selected by the user. See Selection.onSetFocus( ), TextField.onKillFocus( ), and TextField.onSetFocus( ).
For author-time text fields, many TextField object properties can be set through the Property inspector interface; any options set at authoring time are reflected at runtime by the corresponding TextField instance's properties.
We can respond to changes in a text field's scroll position or content via the TextField events onChanged( ) and onScroller( ).
To format the appearance of text in a text field, we use either a TextFormat object or HTML display. For details, see the following TextField entries: embedFonts, getFontList( ), getTextFormat( ), getNewTextFormat( ), html, htmlText, setFontList( ), setTextFormat( ), setNewTextFormat( ), and textColor( ), and see the TextFormat class entry. The border and background color of a text field's containing rectangle can also be set; see the following TextField entries: border, borderColor, background, and backgroundColor.
Prior to Flash 6, text fields were not formally accessible as objects. As of Flash 6, the TextField class lets us manipulate text fields much as we do movie clips, by setting position, size, rotation, etc. In fact, many text field properties are borrowed directly from the MovieClip class (for example, _x, _y, _width, _height, and _rotation). For the sake of backward compatibility, text field properties corresponding to movie clip properties that existed prior to Flash 6 are prefixed with an underscore (_). To conform more closely with other ECMA-262 languages, properties that are completely new in Flash 6 do not use the underscore convention.
In Flash 6, built-in properties set on TextField.prototype are not inherited by text field instances, so custom default values cannot be assigned for all text fields in a movie:
// This attempt to give all text fields a border by default fails in Flash 6 TextField.prototype.border = true;
MovieClip.createTextField( ), the TextFormat class; Chapter 17