Autoscaling TLF Text in AS3
I'm needing an actionscript solution that will allow dynamic text to drop into a text box with pre-determined dimensions (x, y, width, height), and then will scale the text up or down so that it is as large as it can be within those dimensions without scrolling. Wordwrap would be automatic, and there would not be any paragraph breaks.
I have a working model using Flash's Classic text, but I would like to be able to utilize the in-line styling that TLF provides. I just don't quite have my mind开发者_StackOverflow中文版 wrapped around all the TLF features yet.
Does anyone know if there is an already existing solution to this situation - or could perhaps steer me in the right direction?
@phil: This should help: http://aaronhardy.com/flex/size-text-to-container/
Online demo, right click for source code.
Hm - this should work, but I am not sure over how precise the TLF font size is... Anyways:
newFormat:TextFormat = new TextFormat();
newFormat.size *= myText.width / myText.textWidth;
myText.setTextFormat(newFormat);
Now - this basically creates a TextFormat object and sets it's font size to myText's (the TextField) container width (the maximal width) divided by the actual text width. Again - if the TLF font size is NOT so precise, the size line must be:
newFormat.size *= Math.round(myText.width / myText.textWidth * 100) / 100;
100 means it is rounded to hundredths.
edit: I really believe this method is not only much simpler, but also efficient... I mean - that is the point of TextField.textWidth...
精彩评论