Greetings to all!
Today, I decided to talk about TextFormat as I have been asked more then once some help on how to apply a chosen font to a text field with Actionscript 3.0.
I will start by telling you the basics:
1. " create the needed font and insert it in an Array
2. " create a text field
3. " create an instance of TextFormat
4. " assign the TextFormat to the text field
5. " assign the text to the text field
Without losing any time, let us take a look at a concrete example"
I create a FLA and I save it as "main.fla".
Into which, I create a new font in library as seen in
this tutorial.
I create the Document Class, an AS file saved as "Main.as", implemented the following way:
Code:
package
{
import flash.display.MovieClip;
import flash.text.Font;
import flash.text.TextField;
import flash.text.TextFormat;
public class Main extends MovieClip
{
private var field_txt:TextField;
private var format:TextFormat;
private var fonts_array:Array;
public function Main()
{
init();
}
private function init():void
{
Font.registerFont(Monaco);
fonts_array=Font.enumerateFonts(false);
createField();
createFormat();
setFormat();
setText();
}
private function createField():void
{
field_txt=new TextField();
addChild(field_txt);
field_txt.x=20;
field_txt.y=20;
field_txt.selectable=false;
field_txt.embedFonts=true;
field_txt.multiline=true;
field_txt.width=300;
field_txt.wordWrap=true;
}
private function createFormat():void
{
format=new TextFormat();
var font:Font=fonts_array[0];
format.font=font.fontName;
format.color=0x333333;
format.size=14;
}
private function setFormat():void
{
field_txt.defaultTextFormat=format;
}
private function setText():void
{
field_txt.text="Lorem Ipsum is simply dummy text of the printing and typesetting industry"+
"Lorem Ipsum has been the industry's standard dummy text ever since the 1500s,"+
" when an unknown printer took a galley of type and scrambled it to make a type specimen book."+
"It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged."+
"It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, "+
"and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.";
field_txt.height=field_txt.textHeight+10;
}
}
}
to obtain the following result:
Let us analyse the code:
Properties
an instance of TextField
private var field_txt:TextField;
an Array
private var fonts_array:Array;
Following the basic steps explained above,
1 " create the needed font and insert it in an Array
Font.registerFont(Monaco);
fonts_array=Font.enumerateFonts(false);
2 " create a text field
I call the method createTextField and:
I create the field
field_txt=new TextField();
addChild(field_txt);
I position it
field_txt.x=20;
field_txt.y=20;
I declare that it is not selectable
field_txt.selectable=false;
I impost the property embedFonts to true
field_txt.embedFonts=true;
I declare it as multi line
field_txt.multiline=true;
I impost the width of the text field
field_txt.width=300;
I impost the property wordWrap to true
field_txt.wordWrap=true;
3 - create an instance of TextFormat
I call the method createFormat
I create a new textFormat
format=new TextFormat();
I assign the font to "format"
var font:Font=fonts_array[0];
format.font=font.fontName;
I assign a colour to "format"
format.color=0x333333;
I assign a size to "format"
format.size=14;
4 - assign the TextFormat to the text field
I call the method setFormat which assign the textFormat to the text field this way:
field_txt.defaultTextFormat=format;
5 - assign the text to the text field
I call the method setText
I assign the text
field_txt.text="......";
I assign a maximum width to the text field
field_txt.height=field_txt.textHeight+10;
Source files: