Kamis, 21 Mei 2015

Text formatting

Okay, what about things like text colors, size, and even the font? Formatting options are handled by a separate Flash class, the TextFormat class. To use it we need to create an instance of the class, and then apply to to the text in our text box.

First we create an instance of the class :

    var myFormat:TextFormat = new TextFormat(); 

Now let's add some formatting:

    myFormat.color = 0xAA0000;  
    myFormat.size = 24;  
    myFormat.italic = true;   
    myFormat.align = TextFormatAlign.CENTER 

Finally, apply the format to the text in the text box :

    myTextBox.setTextFormat(myFormat);

You should now have 24px red italic centered text in your text box. Other properties can be found in the Adobe Flash documentation, including .bold, .indent, .kerning, .leading, .leftMargin, .letterSpacing, and so on.

    myFormat.color = 0xAA0000;  
    myFormat.size = 24;  
    myFormat.italic = true;   
    myFormat.align = TextFormatAlign.CENTER
    myFormat.font = "Helvetica";
    myTextBox.setTextFormat(myFormat);


That will work, as long as you (or the user) has Helvetica installed. But what if they don't? Or what if you want to use a more obscure font? In that case, you must embed the font in the project.
Open your library panel and click on the options menu in the upper-right-hand corner of the panel. From the menu choose New Font.
A dialog box will open. Simply select the font you want to use from the "font" drop down menu. Then click the "Advanced" button in CS4, or press the "ActionScript" tab in CS5. In the expanded window check "Export for ActionScript." Note that the "Class" field show display a default name for the font ("Font1"). You can change it, but for now, let's just go with it. Click Okay. A warning message will likely pop up, but don't worry, simply click Okay.
Now we need to create an instance of the font class we just created, and then apply it to the format object. Add this to the code:

    myFormat.color = 0xAA0000;  
    myFormat.size = 24;  
    myFormat.italic = true;   
    myFormat.align = TextFormatAlign.CENTER
    // myFormat.font = "Helvetica";
    var myFont = new Font1();    
    myFormat.font = myFont.fontName;   
    myTextBox.setTextFormat(myFormat);
    myTextBox.setTextFormat(myFormat); 

Note that I commented-out the previous line where I set the font to Helvetica. You could also delete it altogether.
One more thing; we need to add a couple of lines to insure that the textField will properly display the font. Put these with the other myTextBox properties :

        myTextBox.background = true;  
        myTextBox.backgroundColor = 0xFFFF00;
        myTextBox.embedFonts = true;
        myTextBox.antiAliasType = AntiAliasType.ADVANCED;


Lokasi: United States

0 komentar:

Posting Komentar