Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Auto Resize Dynamic Text Font as3

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.

like image 794
echez Avatar asked Dec 16 '25 20:12

echez


1 Answers

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 );
}
like image 139
NHubben Avatar answered Dec 19 '25 23:12

NHubben



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!