Dynamic FontSize - Animate CC

Hi guys,
first of all, thanks for the great forum. It answered a lot of my questions. But there are still 2 left.
I would like to make the font size dynamic (not only squezzing X). So 30pt is the font and if the text is longer than the textfield, to make the font size/text smaller automatic and proportional.

I found solutions with .scaleX and .scaleY and put them inside the Action Tab of the textbox in Animate but then i have the problem, that the position of the text is wrong. It should stay right aligned and centered vertical.

Do you have an idea how to solve it?

… and a follow-up question: What do i have to do, to outsource this code to a AS file to use it for more textfield? Is it enough to reference the AS file as Class in the FLA properties? Where i have to point to the desired textfields?

Thank you in advance!

I don’t know about Animate CC, but if you know javascript you can easily adjust font size of the text field to match the width of the container.

Hello,
Want to auto-fit dynamic text?
this function can work for you.

first sets the letterspacing in the given range, then set the font to min. shrinks down to (and averages by the y-value)

function scaleTextToFit(txt: TextField, txt_y:int, maxfont: int, minfont:int, spacing: int, maxspacing:int):void
		{  
			 var f:TextFormat = txt.getTextFormat();
			 f.letterSpacing = spacing;
			 f.size = maxfont;
			 txt.y = txt_y;

 			 txt.setTextFormat( f );
	 
			 while ( txt.textWidth > txt.width - 4 ) 
			 {    
				 f.letterSpacing = int( f.letterSpacing ) - 1;    
				 if (f.letterSpacing < maxspacing) {
					f.letterSpacing = maxspacing;  
					break;  //minimum spacing
				 }
				 txt.setTextFormat( f );  
			 }
			 
			 while ( txt.textWidth > txt.width - 4 || txt.textHeight > txt.height - 4 ) 
			 {    
				 f.size = int( f.size ) - 1;    
				 if (f.size < minfont) {
				    f.size = minfont;
					break;  //minimum size
				 }
				 txt.setTextFormat( f );  
			 }

			 txt.y += Math.round((txt.height - txt.textHeight) / 2);
		}