In case Flash no longer exists; a copy of this site is included in the Flashpoint archive's "ultimate" collection.

Dead Code Preservation :: Archived AS3 works from wonderfl.net

Weirdness of the height property of TextField

http://level0.kayac.com/2009/08/post_39.php
Get Adobe Flash player
by 9re 25 Aug 2009
/**
 * Copyright 9re ( http://wonderfl.net/user/9re )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/rpNz
 */

package  
{
	import flash.display.Sprite;
	import flash.display.StageScaleMode;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFormat;
	import flash.text.TextFormatAlign;
	// http://level0.kayac.com/2009/08/post_39.php
	public class TextFieldAutoSizeExample_0 extends Sprite
	{
		private const MAX_SIZE:int = 32;
		private const MIN_SIZE:int = 8;
		
		public function TextFieldAutoSizeExample_0() 
		{
			var tf:TextField = new TextField();
			var tfm:TextFormat = new TextFormat("_sans", 12, 0, null, null, null, null, null, TextFormatAlign.JUSTIFY);
			var i:int = MAX_SIZE;
			var str:String = "Flash Player Bug??\n\nFlash Player sometimes fails to get proper height of TextField. This may issue from internal text rendering process. You can avoid this problem by executing getter method - the height property is internally implemented as getter and setter method - before using the height property. If you comment out the line 39, the code fails to work properly.";
			
			stage.scaleMode = StageScaleMode.NO_SCALE;
			
			tf.autoSize = TextFieldAutoSize.LEFT;
			tf.wordWrap = true;
			tf.multiline = true;
			tf.text = str;
			tf.width = 300;
			tf.height = 1;
			addChild(tf);
			
			var tfHeight:int;
			
			while (i >= MIN_SIZE) {
				tfm.size = i--;
				tf.setTextFormat(tfm);
				
				// the follwing line avoids the problem
				tf.height;
				tfHeight = tf.height;
				
				if (tfHeight < 300)
					break;
			}
			
			
			tf = new TextField();
			tf.width  = 200;
			tf.text = "fontSize: " + i.toString() + " tf.height: " + tfHeight.toString();
			tf.x = 310;
			addChild(tf);
		}
		
	}
	
}