1
votes

I have a dynamic text field on the stage, and i want the font size of the text to auto enarlge as per the stage size (which is 800x600). How can i do this?

If i manually enter big font size, and the text is much larger in lenght then it will clip off. Any ideas on this?

I am using Actionscript 3.

Thanks!

2

2 Answers

1
votes

there are a couple of ways to accomplish this. if you want to use the same size font regardless of how much text you produce, you should use wordWrap = true on your text field. if you want to adjust the font depending on the amount of text in the field, it becomes a bit more tricky but not impossible. I would do something like this

var maxFont:int = 72;
var minFont:int = 9;
var currentSize:int;
var maxTxtWidth:int = 550;
var incTxt:String;

var tf:TextField = new TextField;
tf.autoSize = "left";
currentSize = maxFont;
while(tf.width > maxTxtWidth){
    tf.htmlText = '<font size="'+currentSize+'">'+incTxt+'</font>';
    if(currentSize == minFont){
       break;
    }  
    currentSize --;
}

then addchild etc as you normally would. there are other ways to do it, using styles, or textFormat but this is a fast solution, and the first to come to mind. Note that this particular syntax will only work properly for a single line of text. You can tweak it a bit and make it suitable for multiline. if you do use this technique, please clean it up, this is just spit out of my head directly.

1
votes

ok with the help of the code from @NappingRabbit i concluded my own working code.

My main objective was to auto-resize the font size with respect to the stage size (550x400). If the text length is few then show it as big as possible. And if the ext length is a lot then lower the font size.

Here's my working code for others who are seeking the same. (Flash CS4 / AS3)

var tf:TextField = new TextField();
var size:int = 72;
var foundLargeText:Boolean = false;
var txt:String = "Starting.... Lorem ipsum dolor sit amet, vim laudem argumentum te. Mel nihil nobis oratio ea. Ex sed dolores deterruisset, qui in eius liber. Ne qui minim iracundia, dictas saperet ut cum, id nobis conceptam persequeris sea. Eum at ferri dolor denique, mel audiam fabulas quaestio at. Illum voluptua facilisis nec eu, nam ea quem putent. Elitr aliquam mea ne. In eam iudico petentium scriptorem. Pro mucius oporteat te, impetus scriptorem sea ne, quo graecis menandri rationibus ex. Quando repudiare adipiscing mel cu, has insolens platonem te, eu quando scripta accusata quo. Idque dictas vis eu, an eripuit delenit conclusionemque pri. Et incorrupte signiferumque has, alia bonorum nec id, sea scripta insolens expetendis ne. This is end...";

tf.autoSize = "right";
tf.wordWrap = true;
tf.width = 540;
tf.htmlText = "<font size='"+size+"'>"+txt+"</font>";
tf.border = true;
while(tf.textHeight>=400){
    size--;
    foundLargeText = true;
    tf.htmlText = "<font size='"+size+"'>"+txt+"</font>";
}
trace("using size: "+size);
if(foundLargeText){
    tf.htmlText = "<font size='"+size+"'>"+txt+"</font>";
}
addChild(tf);

Thanks everybody!