views:

24

answers:

2

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

The actual text that will populate the dynamic text field is 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 2.

Thanks

+1  A: 

This should work:

function updateFontSize(tField:TextField, defaultSize:Number) {
var tFormat:TextFormat = new TextFormat();
tFormat.size = defaultSize;
tField.setTextFormat(tFormat);

var size:Number = defaultSize;
while((tField.textWidth > tField._width || tField.textHeight > tField._height) && size > 0) {
    size = size - 1;
    tFormat.size = size;
    tField.setTextFormat(tFormat);
}}

Call this function whenever you change your text. First argument for this function is the text field. The second is the font size you would prefer (it will be reduced if it's too large).

SomeBloke
+1  A: 
PatrickS