Auto change text size to fit text field


/ Published in: ActionScript 3
Save to your folder(s)



Copy this code and paste it in your HTML
  1. function Autosize(txt:TextField):void
  2. {
  3. //You set this according to your TextField's dimensions
  4. var maxTextWidth:int = 145;
  5. var maxTextHeight:int = 30;
  6.  
  7. var f:TextFormat = txt.getTextFormat();
  8.  
  9. //decrease font size until the text fits
  10. while (txt.textWidth > maxTextWidth || txt.textHeight > maxTextHeight) {
  11. f.size = int(f.size) - 1;
  12. txt.setTextFormat(f);
  13. }
  14.  
  15. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.