Text Formatting & AutoSizing
Posted on 16. Apr, 2009 by Michele in ActionScript 3 Snippets
private function addTextCaption():void{
if(textCaption != null){
removeChild(textCaption);
}//remove if it exists
textCaption = new TextField();
var font:Verdana = new Verdana(); //linkage name in library
var format:TextFormat = new TextFormat(); //should have separate formatting function
format.font = font.fontName;
format.color = textColorVar;
format.size = infoFontSize;
format.align = TextFormatAlign.LEFT;
textCaption.x = thumbnailWidth + textBorder;
textCaption.y = imageHeight + headingHeight;
textCaption.width = stage.stageWidth – thumbnailWidth;
textCaption.height = 200; //don’t need height if autosize single or multiline text
textCaption.multiline = true;
textCaption.wordWrap = true;//must be set else cuts off
//textCaption.autoSize = TextFieldAutoSize.LEFT;
textCaption.antiAliasType = flash.text.AntiAliasType.ADVANCED;
textCaption.selectable = false;
textCaption.embedFonts = true;
addChild(textCaption);
textCaption.defaultTextFormat = format;
textCaption.text = imageCaptionArray[tempCounter];
}//addTextCaption
NOTES:
To autosize height do not specify a height but must include autosize:
textCaption.wordWrap = true;
textCaption.autoSize = TextFieldAutoSize.LEFT;
textCaption.x = textBoxXPos;
textCaption.y = textBoxYPos;
textCaption.width = stageWide – textBoxXPos;
//textCaption.height = stageHigh – textBoxYPos;//not allowed with autoSize
For width autosize leave out width obviously.


