The TextStyleClass - The Class Script (continued)

In the following we write the other required functions. First we write a setter to embed fonts in the textfield. We also add to set the antiAliasType to "advanced", which is only possible with embedded fonts. This will give a crispier look.

/*
this function needs to be called from the movie to embed the 
textfield. If not called the textfield will not be embedded. The syntax
is
FontEmbed_instance.embedding = true;

This function will also embed a textfield from the movie library.
*/
	       public function set embedding (txtEmbed:Boolean):void
	       {
	               my_txt.embedFonts = txtEmbed;
	               my_txt.antiAliasType = "advanced";
	       }
	       

In the next function we will load the stylesheet and also get the path/url for the XML file. This is a typical text-loading function where we have an event listener to monitor when the stylesheet is loaded.

/*
This is the third function to be called from the movie. It has two
parameters for the XML file and the stylesheet URLs.
*/
	       public function loadFiles (xmlFile:String, myStyle:String):void
	       {
	               //
	               // we first load the stylesheet
	               //
	               stSheet = myStyle;
	               xFile = xmlFile;
	               var request:URLRequest = new URLRequest(stSheet);
	               var stLoader:URLLoader = new URLLoader();
	               stLoader.addEventListener (Event.COMPLETE, loadSTSheet);
	               stLoader.load (request);
	       }
	       

Once the stylesheet is loaded we parse it and prepare to load the XML file, which has the text, which we want to show. We do that in the same way, as we have loaded the stylesheet.

	       //
	       // this function is executed when the stylesheet is loaded.
	       //
	       private function loadSTSheet (e:Event):void
	       {
	               //
	               // the stylesheet data are parsed.
	               //
	               style.parseCSS (e.target.data);
	               //
	               // now we load the XML file.
	               //
	               var request:URLRequest = new URLRequest(xFile);
	               var xmlLoader:URLLoader = new URLLoader();
	               xmlLoader.addEventListener (Event.COMPLETE, gotoParser);
	               xmlLoader.load (request);
	       }
	       

Now we can focus on the display of the text in the textfield. We create an XMLDocument class instance, because it is very simple to parse the XML for our purpose this way. We then add the stylesheet properties to the textfield. We then show the whole XML document in the textfield. But we need to convert the XML to a string. We add a scrollbar. However, when the text height is smaller than the textfield height we will hide the scrollbar.

	       //
	       // this function is executed when the XML file is loaded.
	       //
	       private function gotoParser (e:Event):void
	       {
	               //
	               // we create a new XMLDocument instance
	               //
	               xmlData = new XMLDocument(e.target.data);
	               xmlData.ignoreWhite = true;
	               //
	               // we now give style format to the textfield.
	               //
	               my_txt.styleSheet = style;
	               //
	               // we show the text.
	               //
	               my_txt.htmlText = xmlData.toString();
	               //
	               // now we add a UIScrollbar.
	               //
	               var mySb:UIScrollBar = new UIScrollBar();
	               mySb.direction = "vertical";
/*
We place and size the scrollbar according to the width and
height of the textfield. We tarhet the scrollbar to the 
textfield.
*/
	               mySb.setSize (my_txt.width, my_txt.height);
	               mySb.move (my_txt.x+my_txt.width, my_txt.y);
	               addChild (mySb);
	               mySb.scrollTarget = my_txt;
/*
We set the visibility to false. Then the scrollbar will only
be shown when the text height is bigger than the textfield height.
*/
	               mySb.visible = false;
	               if (my_txt.textHeight>my_txt.height)
	               {
	                       mySb.visible = true;
	               }
	       }
	}
}

And that finishes the class. We will now focus on the movie itself.

previous  |   next