开发者

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 complete开发者_运维问答ly display within the text field's dimensions.

Any ideas on how I can accurately perform this?

Also, I am using AS 2.

Thanks


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


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;
   }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜