Dynamic Text

I l @ ve RuBoard

You can display text in Flash by using dynamic text fields. These are like the input text fields you learned about in Hour 9, "Getting Input From the User," but they cannot be used for direct user input.

Instead, dynamic text fields can be used to display text with different styling and fonts.

Dynamic Text Options

You create a dynamic text field by using the Text tool just like you would for an input text field. However, instead of choosing "Input Text", choose "Dynamic Text", as shown in Figure 10.1.

Figure 10.1. The Properties panel for a dynamic text field.

graphics/10fig01.jpg

The next step is to link this field to a variable, just like you do with input text. In Figure 10.1, you can see that I linked the field with a variable named " myText ". Now, whenever I change the variable " myText ", the dynamic text field will change to mirror it.

You can also choose from a variety of other options. You can make it "Single", "Multiline", or "Multiline No Wrap" to reflect how you want the field to handle long lines. For a bit of information that should only take up one line, such as the score in an arcade game, you should use "Single". In most other cases, you should use "Mutliline".

Across the top of the Properties panel, you will see some controls that let you set the font, size , and style of the text in the field. You can also click the Format button to change the indent, line spacing, and margins.

graphics/clock.gif

Although you can change the x and y position of the field in the Properties panel, don't change the width and height. Doing so does not make the text area larger, but instead stretches any text inside it. If you want to change the width and height of a dynamic text field, do so by grabbing the bottom-right corner of the field in the work area and dragging.


In the middle of the Properties panel, you will see three little buttons . The first, which looks like an "Ab" lets you decide whether the user can select the text in the field.

Select this option if the user is supposed to be allowed to select and copy the text. In most cases, you will want it off. The next little button determines whether you can use HTML formatting in the field. We'll look at HTML formatting in the next section. The third button places a border around the field.

The Character button in the Properties panel lets you decide which characters of the font will be saved with the movie when you publish it.

Say that you have a dynamic text field and you want to put "Hello World." into it. First, you need to set the Var property in the panel to a variable name , such as myText . After that is done, don't worry about the field, just set the variable.

 myText = "Hello World."; 

It's that easy. But you can do much more with dynamic text fields.

HTML Formatting

The easiest way to add formatting to dynamic text fields is to use HTML. First, you'll need to set the field to accept HTML. You do this by clicking the little Render Text as HTML button in the middle of the Properties panel. It looks like a "<>". When this is turned on, you can use a few select tags to change your text. Here is an example:

 myText = "This text is <B>bold</B>.<BR>"; myText += "This text is <I>italic</I>.<BR>"; myText += "This text is <U>underlined</U>.<BR>"; myText += "This text is <FONT COLOR='#FF0000'>red</FONT>.<BR>"; myText += "This text is <FONT FACE='Arial Black'>Arial Black</FONT>.<BR>"; myText += "This text is <FONT SIZE='24'>large</FONT>.<BR>"; myText += "This text is <A HREF='link.html'>linked</A>.<BR>"; 

This code builds a string of text that includes some HTML tags. Figure 10.2 shows the result. You can see this example in 10htmltext.fla.

Figure 10.2. This styled text was created in ActionScript with HTML-like tags.

graphics/10fig02.jpg

Notice that it is not a complete HTML page with a <BODY> tag or anything, so it is not true HTML. However, by using regular tags such as <B>, <I>, and <FONT>, it is easy to learn and remember what works. Here is a complete list of tags that work in Flash 6:

  • <B></B> : Bold

  • < I></I> : Italics

  • <U></U> : Underline

  • <FONT FACE='Arial Black'></FONT> : Font

  • <FONT SIZE='24'></FONT> : Set text size

  • <FONT COLOR='#XXXXXX></FONT> : Color text

  • <A HREF=''></A> : Hypertext link to a Web page

  • <P></P> : Define a paragraph

  • <BR> : New line

The hyperlink is the most functional of all the tags. On the one hand, it doesn't display blue and underlined like tags do in browsers, but it does work the same. If you click on a link, the page it refers to will load. If you want to color or style the text, you can apply those tags independently of the <A HREF> tag.

TextFormat Object

Another way to set the font, size, and style of dynamic text is to use a TextFormat object. To do this, you create a variable and define it as an instance of TextFormat . Then, you can set various properties of this variable. In this example, the font is set to Arial Black, the size is set to 26, and the color is set to red. The dynamic text field has been named textInstance . We have never named a text field before, but you can see in Figure 10.1 where the name goes.

 myFormat  = new TextFormat(); myFormat.font = "Arial Black"; myFormat.size = 36; myFormat.color = 0xFF0000; textInstance.setTextFormat(myFormat); 
graphics/book.gif

You have to use various methods in Flash to define colors. It depends on what part of ActionScript you are using at the time. In this example, 0xFF0000 is used, where the initial 0x defines this as a hexadecimal color definition. However, in the earlier HTML tag, we used #FF0000 to define the same color.


You can see this code in action in the example movie 10formattext.fla. The setTextFormat command can also accept a second and third parameter to tell it the first and last characters to apply the format to.

You can use text format objects like style sheets in HTML. You can define several of them at the start of your movie and then apply those formats as you need them throughout the movie.

I l @ ve RuBoard


Sams Teach Yourself Flash MX ActionScript in 24 Hours
Sams Teach Yourself Flash MX ActionScript in 24 Hours
ISBN: 0672323850
EAN: 2147483647
Year: 2002
Pages: 272

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