Auto Resize Dynamic Text Font as3

echez picture echez · Jun 22, 2011 · Viewed 10.8k times · Source

I have dynamic text field that must be a fixed width and height.

The actual text that will populate the dynamic text field is a variable.

What I would like to do is to reduce the font size if the text does not completely display within the text field's dimensions.

Any ideas on how I can accurately perform this?

Also, I am using AS 3.

Answer

NHubben picture NHubben · Jun 22, 2011

give this a try if you're still looking: (this assumes your TextField is set to "multiline" and is only 1 line high when it inits)

var smallLimit:int = 10;
var format:TextFormat = new TextFormat();

tf.text = "THIS IS WAY TOO LONG";

var testSize:int = 200;
while( testSize > smallLimit ){

    updateFormat( testSize );
    //trace( tf.numLines  );

    if( tf.numLines > 1 ){
        testSize--;
    }else{
        testSize = smallLimit;
    }
}

function updateFormat(size:int):void{
    format.size = size;
    tf.setTextFormat( format );
}