Steve
Steve

Reputation: 55635

Dynamic Text - Auto Resize of Font

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

Upvotes: 2

Views: 3487

Answers (2)

PatrickS
PatrickS

Reputation: 9572

Following on SomeBloke's code, here's another approach using scaling

//set the TextField width & height 
var fixedWidth:Number = 200;
var fixedHeight:Number = 24;

function scaleTextToSize(tField:TextField, defaultScale:Number) 
{
   //You can fine tune the amount of scaling here
   var amount:Number = .1;
   var scale:Number = defaultScale;

   while((tField.width > fixedWidth || tField.height > fixedHeight) ) 
   {
      scale -= amount;
      tField.scaleX = tField.scaleY = scale;
   }
}

Upvotes: 1

Circuit in the wall
Circuit in the wall

Reputation: 549

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).

Upvotes: 3

Related Questions