Download (direct link):
tLabel.multiline = true;
402 Part V ¦ The Movie Classes
Word wrapping is another consideration. When you are working with multiline text you may want to have the text wrap to the next line when it reaches the extent of the bounding box width. In order to achieve that with ActionScript, you need only set the wordwrap property to true.
tLabel.wordWrap = true;
Otherwise, as long as wordwrap i s false the text will continue to extend past the width of the bounding box until a line break is encountered.
Resizing a TextField Object
Because the text in a TextField object is subject to change dynamically, it is not necessarily going to be the case that you’ll know the dimensions of the text during authoring time. Instead, you may want to change the dimensions based on the content.
When we discuss resizing a TextField object, we are distinguishing between resizing and scaling. If you set the _xscale and/or _yscale property for a TextField object, the text that it displays will also scale. If you want to resize a TextField object, that means you want to change the dimensions of the bounding box while maintaining the font size of the text.
The simplest type of resizing involves telling Flash to automatically resize the TextField to accommodate any text that is assigned to it. You can do that by assigning the corresponding value to the object’s autoSize property. The following is a list of the possible values:
¦ none: This is the default setting, and it means that the TextField object does not resize automatically.
¦ left: This means that the object automatically resizes to the content using the upper-left corner of the TextField instance as the fixed point.
¦ right: This means that the object automatically resizes as with the setting of left, but it uses the upper-right corner as the fixed point instead of the left corner.
¦ center: This means that the object automatically resizes as with a setting of left or right, but it uses the upper center as the fixed point.
Note If you assign a Boolean value to the autoSize property it will work. A value of true i s syn-
onymous with left and a value of false is synonymous with none. However, we encourage you to use the correct string values, especially considering ActionScript's move toward stronger typing.
In order to understand how the autoSize property affects resizing, it is helpful to see an actual example. The following code creates three TextField objects, adds text to them, and tells them to resize in different ways:
this.createTextField("tLabel1", this.getNextHighestDepth(), 0, 0, 0, 0); tLabel1.text = "Label one"; tLabell.autoSize = "left";
this.createTextField("tLabel2", this.getNextHighestDepth(), 0, 25, 0, 0); tLabel2.text = "Label two"; tLabel2.autoSize = "right";
this.createTextField("tLabel3", this.getNextHighestDepth(), 0, 50, 0, 0); tLabel3.text = "Label three"; tLabel3.autoSize = "center";
Chapter 17 ¦ The TextField and Selection Classes
Figure 17-3 shows what the preceding code displays when tested. The vertical line is added where x is 0 to give you a reference point.
Figure 17-3: An example of three TextField objects with different autoSize values
You can also, of course, simply assign new values to the _width and _height properties.
That will cause the bounding box to resize to specific pixels widths. The textWidth and textHeight properties return the pixel width and height of the text contained within a TextField object and, as such, can be used for some simple resizing effects. Flash places a two-pixel margin around the text within a TextField object, and so when you use the textWidth and/or textHeight properties, be aware that you’ll probably want to add four pixels to those values. The following code creates a TextField object sized to match the width of the text, but is set to a fixed 20 pixels in height:
this.createTextField("tSample", this.getNextHighestDepth(), 0, 0, 0, 0);
tSample.text = "This is\nsome\nsample\ntext.";
tSample._height = 20;
tSample._width = tSample.textWidth + 4;
The preceding example would be useful if, for example, you wanted to create a TextField that is a specific number of pixels in one dimension but that fits the text exactly in the other dimension. You can then use the scrolling properties to scroll the text as shown in the section “Scrolling Text” later in this chapter.
The textWidth and textHeight properties provide very simplistic information about the text dimensions within a TextField object. For more complex information, you can use the
getTextExtent() method of a TextFormat object as described in Chapter 18.
Making Text Non-Selectable
The default setting for text is that it is selectable. That means that the user can use the mouse to highlight and copy the text as well as place the cursor within the instance for input (in the case of input text). In some situations that is preferable. For example, input text must be selectable, and if you want the user to be able to copy and paste something you should make sure it is selectable. However, you might well want other text to be nonselectable. Labels, titles, and so on, should probably be made nonselectable. And when text is used within a button it should definitely be made nonselectable so that it doesn’t interfere with the state detection of the button.