ActionScript 3 Text Field Alpha

Looking through the documentation for applying alpha transparency and rotation to a text field, I realized I need to learn something about embedding fonts. Searching for a solution led me to a solution: Making Alpha Work on AS3 Dynamic Text Fields.

This doesn’t work:

package {

	import flash.display.Sprite;
	import flash.text.TextField;

	public class TextAlpha extends Sprite {

		private var textFld:TextField = new TextField();

		public function TextAlpha() {
			textFld.text = "This is a text field";
			textFld.alpha = .2;
			addChild(textFld);
		}
	}
}

So, what to do? The Adobe Developer Connection resource may be the best place to start:

But how can this be accomplished without Flash, using the Flex 3 SDK?

I was never able to successfully use the [Embed] metatag to compile a SWF file for embedding and loading fonts. I created a SWF with an embedded font in Flash CS3, then I created the following class to load the embedded font and display and transform the text using the loaded font. If no font is found, the error handing event displays the text in Arial with no transformation.

package {

	import flash.display.Sprite;
	import flash.display.Loader;
	import flash.display.LoaderInfo;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.net.URLRequest;
	import flash.system.LoaderContext;
	import flash.text.Font;
	import flash.text.TextField;
	import flash.text.TextFormat;
	import flash.text.TextFieldAutoSize;

	public class EnumerateFonts extends Sprite {

		private var t:TextField = new TextField();
		private var f:TextFormat = new TextFormat();
		private var font:Font;
		private var fontFile:String = "Helvetica.swf";

		public function EnumerateFonts() {

			// trace loaded fonts
			var fonts:Array = Font.enumerateFonts();

			// to list all available fonts, use the following instead:
			// var fonts:Array = Font.enumerateFonts(true);

			if (fonts.length == 0) {
				t.text="No fonts are loaded";
			} else {
				for (var x:uint=0; x<fonts.length; x++) {

					font = fonts[x];
					trace("fontName : " + font.fontName);
					trace("fontStyle : " + font.fontStyle);
					trace("fontType : " + font.fontType);
				}
			}

			//load font
			var loader:Loader = new Loader();

			loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
			loader.contentLoaderInfo.addEventListener(Event.COMPLETE,onLoaded);

			loader.load(new URLRequest(fontFile));
		}

 	 	private function ioErrorHandler(event:IOErrorEvent):void {
 	 	 	trace("No fonts loaded: " + event);
			fontNotLoaded();
 	 	 }

		private function onLoaded(event:Event):void {

			// trace loaded swf and fonts
			trace("SWF loaded: " + fontFile);
			var fonts:Array = Font.enumerateFonts();
			for (var x:uint = 0; x < fonts.length; x++) {

				font = fonts[x];
				trace("fontName : " + font.fontName);
				trace("fontStyle : " + font.fontStyle);
				trace("fontType : " + font.fontType);

			}
			fontLoaded();
		}

		private function addTextField() {
			// default text field properties
			t.defaultTextFormat = f;
			t.x = 40;
			t.y = 40;
			t.background = true;
			t.autoSize = TextFieldAutoSize.LEFT;
			addChild(t);
		}
		
		private function fontLoaded() {
			// Text format
			f.font = font.fontName; // "HelveticaNeueLT Std Lt"
			f.size = 48;
			
			// Text field
			addTextField();
			t.text = "The font is loaded:\n";
			t.text += font.fontName;
			t.embedFonts = true;
			t.alpha = .5;
			t.rotation = 10;
		}

		private function fontNotLoaded() {
			// Text format
			f.font = "Arial";
			f.size = 24;

			// Text field
			addTextField();
 	 	 	t.text = "No fonts loaded" + "\n";
			t.text += "Default font: Arial";
		}
	}
}
Advertisements

About this entry