开发者

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 dimension开发者_Go百科s.

Any ideas on how I can accurately perform this?

Also, I am using AS 3.


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


try this :

var tfspecial:int = tfFontSize + tfInterLine;

while (tf.numLines < tf.height * tfspecial) {
    tf.height += tfspecial;
}


I wrote this function but i dont know why it works only once on dynamic text. When replace content on tf it nocorectly sets tfHeight. but it works if you set constant tfHeight value insted var tfHeight = tf.height;

function fitFontSize(tf:TextField){
    //pobieramy oktualna wysokosc pola tekstowego
    var tfHeight = tf.height;
    // ustawiamy automatyczne rozszerzanie sie pola ze wzgledu na zawartosc
    tf.autoSize =  TextFieldAutoSize.LEFT ;
    // tworzymy obiekt tekst format
    var myFormat:TextFormat = new TextFormat();
    //ustawiamy poczatkowa wielkosc fonta na 100
    var tfFontSize:Number =100;
    // przypisujemy do obiektu
    myFormat.size = tfFontSize;
    tf.setTextFormat(myFormat);
    /*Z PETLI FOR POMNIEJSZAMY SUKCESYWNIE CZCIONKE O JEDEN
PIKSEL DO MOMENTU GDY tf TEKSTOWE OSIAGNIE POŻADANĄ 
WYSOKOSC LUB MNIEJSZA*/

    for (var i:int = 0; i<100; i++){
        tfFontSize-=1;
        myFormat.size = tfFontSize;
        tf.setTextFormat(myFormat);
        if (tf.height<= tfHeight){
        break;
        }
    }
}
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜