The TextStyleClass - The Class Script

We will first write the class script before we look at the movie. The script is divided into several functions, a function to create the textfield and give it properties, then a function to embed fonts, a function to load the stylesheet and a function to load, parse and display the XML in the textfield. The script starts with the path declaration follwed by all the classes, which we need to import.

                    package biz.Flashscript
{
	//
	// importing classes
	//
	import flash.display.MovieClip;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	import flash.events.Event;
	import flash.text.TextField;
	import flash.text.Font;
	import flash.text.StyleSheet;
	import flash.xml.XMLDocument;
	import fl.controls.UIScrollBar;
	//
	// This class extends the MovieClip class and can be used with frames in the mmovie.
	//
	public class TextStyleClass extends MovieClip
	{
	

We declare variables as shown by the comments. In the constructor we create an instance of the StyleSheet class.

	       //
	       // the main textfield variable
	       //
	       private var my_txt:TextField;
	       //
	       // holder for the textfield, when textfield is in the movie library
	       //
	       private static  var tHolder:MovieClip;
	       //
	       // stylesheet path/url
	       //
	       private static  var stSheet:String;
	       //
	       // variable to hold the stylesheet information
	       //
	       private var style:StyleSheet;
	       //
	       // XML file path/url
	       //
	       private static  var xFile:String;
	       //
	       // XMLDocument variable to parse the XML file
	       //
	       private var xmlData:XMLDocument;
	       //
	       // constructor, we create a new stylesheet instance here
	       //
	       public function TextStyleClass ()
	       {
	               style = new StyleSheet();
	       }
	       

The first public function is to create, position and format the textfield. There are several parameters, which we set including to make it multiline. We also create options when we set the width and heigh to 0 to autosize it automatically. Further we add an option to have aborder and background color.

/*
function to create and format the textfield. Needs to be called from the movie 
as the first function.
*/
	       public function activateTextfield (textHolder:MovieClip, txt_x:uint, 
	       txt_y:uint, txt_w:uint, txt_h:uint, bdColor:uint, bgColor:uint):void
	       {
/*
if we want to use a textfield inside of a movieclip from
the movie library, we create a new instance of the movieclip
in the movie and add the name as a parameter or otherwise a
new textfield instance is created.
*/
	               if (textHolder==null)
	               {
	                       my_txt = new TextField();
	                       addChild (my_txt);
	               }
	               else
	               {
	                       tHolder = textHolder;
	                       my_txt = tHolder.my_txt;
	               }
	               //
	               // the textfield is placed and formatted
	               //
	               my_txt.x = txt_x;
	               my_txt.y = txt_y;
	               my_txt.multiline = true;
	               my_txt.wordWrap = true;
	               //
	               // if the width and height are BOTH 0 (numeric), the textfield will be autosized.
	               //
	               if (txt_w == 0 && txt_h == 0)
	               {
	                       my_txt.autoSize = "left";
	               }
	               else
	               {
	                       my_txt.width = txt_w;
	                       my_txt.height = txt_h;
	               }
	               //
	               // we can set a border...
	               //
	               if (bdColor!=0)
	               {
	                       my_txt.border = true;
	                       my_txt.borderColor = bdColor;
	               }
	               //
	               // ...and background color.
	               //
	               if (bgColor!=0)
	               {
	                       my_txt.background = true;
	                       my_txt.backgroundColor = bgColor;
	               }
	       }
	       

previous  |   next