Actionscript 3 in Flash player 9 has four different ways to automate the sizing of a text field depending of its contents. The following piece of code shows how each of these "TextFieldAutoSize" constants differ when set prior of setting the content of the text field.
The other constants tested here, the "TextFormatAlign", have no effect if the autosize is other than "none".
package {
import flash.display.*;
import flash.events.Event;
import flash.text.*;
[SWF( backgroundColor = '0x5B5B5B', frameRate = '33', width = '600', height = '400')]
public class TextAutoSizing extends Sprite {
private var median:Shape;
private var autosizes:Array = [
TextFieldAutoSize.CENTER,
TextFieldAutoSize.LEFT,
TextFieldAutoSize.NONE,
TextFieldAutoSize.RIGHT
];
private var alings:Array = [
TextFormatAlign.CENTER,
TextFormatAlign.JUSTIFY,
TextFormatAlign.LEFT,
TextFormatAlign.RIGHT
];
public function TextAutoSizing() {
stage.align = StageAlign.TOP_LEFT;
stage.scaleMode = StageScaleMode.NO_SCALE;
stage.quality = StageQuality.MEDIUM;
this.loaderInfo.addEventListener(Event.INIT, init);
}
private function init(evt:Event):void {
median = new Shape();
median.x = stage.stageWidth / 2;
addChild(median);
var gra:Graphics = median.graphics;
gra.lineStyle(2, 0x0066FF);
gra.lineTo(0, stage.stageHeight);
var k:uint = 0;
for (var i:uint = 0; i < autosizes.length; i++) {
for (var j:uint = 0; j < alings.length; j++) {
var field:TextField = createTextField(autosizes[i], alings[j]);
field.y = 24 * (k) + 10;
field.text = "This text field has autosize: " + field.autoSize + " and align: " + field.defaultTextFormat.align + ".";
addChild(field);
++k;
}
}
}
private function createTextField(auto:String, align:String):TextField {
var format:TextFormat = new TextFormat();
format.align = align;
format.color = 0x000000;
format.size = 12;
var field:TextField = new TextField();
field.background = true;
field.backgroundColor = 0xFFFFFF;
field.border = true;
field.borderColor = 0x000000;
field.autoSize = auto;
field.defaultTextFormat = format;
field.type = TextFieldType.INPUT;
field.x = median.x;
return field;
}
}
}The attached swf file is the output of the code.






















