AS3: Re-Usable TextFormat class

01/02/2010 in RIA View Comments

In this simple tip I provide an easy to use text formatting class. To use this class, simply import the class into your existing document class or other, and then create a variable instance. To call the formatter you will use “textfield.setTextFormat(variable.formatfunction);

The formatting class is below:

package com.riareviver{
 
//FLASH CLASS IMPORT
import flash.display.MovieClip;
import flash.events.*;
import flash.text.TextFormat;
 
public class TxtFormat extends MovieClip {
 
//VAR
public var myTextFormat:TextFormat = new TextFormat();
 
public function TxtFormat() {
//TEXT FORMATTER
myTextFormat.font = "Verdana";
myTextFormat.color = 0xFFFFFF;
myTextFormat.size = 10;
myTextFormat.align = "left";
}
}
}

Below is a sample textfield class using the formatter

package com.riareviver{
 
//FLASH CLASS IMPORT
 
//CUSTOM CLASS IMPORT
import com.riareviver.TxtFormat;
 
public class MyText extends MovieClip {
 
//PUBLIC VARS
 
//PRIVATE VARS
private var myTextField:TextField;
private var txtFormat:TxtFormat = new TxtFormat();
 
public function MyText() {
myTextField = new TextField();
myTextField.x = 5;
myTextField.y = 5;
myTextField.width = 200;
myTextField.text = "My Text";
myTextField.setTextFormat(txtFormat.myTextFormat);
myTextField.selectable = false;
addChild(myTextField);
}
}
}


Click here to see more text formatting properties

Other posts you may find helpful:

blog comments powered by Disqus

Switch to our mobile site