[SOLVED] Scale text to fit textfield

#1
I have tried to use this piece of code in my .as scriptfile

Code: Select all

private static function ScaleDownTextField(Input:String, Field:TextField, initialFieldWidth:int):void
{
	var wi:int = initialFieldWidth;
	Field.autoSize = TextFieldAutoSize.LEFT;
	Field.scaleX = 1;
	Field.text = Input;
	if (wi < Field.width)
	{
		Field.scaleX = wi / Field.width;
	}
}
nothing happens at all to my text, what am I missing?
I guess I have to replace something in the code with some of my field names?
Last edited by tusa on 18 Oct 2017, 16:05, edited 1 time in total.

Re: Scale text to fit textfield

#3
Here is the complete script:

Code: Select all

package
{
	import flash.display.MovieClip;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextField;
	import flash.text.TextFormat;	
	import flash.utils.Timer;
	import flash.utils.setTimeout;
	import flash.events.TimerEvent;
	import flash.events.Event;
	import flash.display.Shape;
	import flash.geom.ColorTransform; 
	import se.svt.caspar.ICommunicationManager;
	import se.svt.caspar.IRegisteredDataSharer;
	import se.svt.caspar.template.CasparTemplate;
	import caurina.transitions.Tweener;

	public class KV17_bundt extends CasparTemplate
	{
		private static function ScaleDownTextField(Input:String, Field:TextField, initialFieldWidth:int):void
		{
			var wi:int = initialFieldWidth;
			Field.autoSize = TextFieldAutoSize.LEFT;
			Field.scaleX = 1;
			Field.text = Input;
			if (wi < Field.width)
			{
				Field.scaleX = wi / Field.width;
			}
		}
		
		override public function postInitialize():void
		{
			bundt_blok_mc.y = 89;
			bundt_blok_mc.mask = bundt_blok_maske_mc;
			colour_line_mc.alpha = 0.0;
			colour_line_mc.y = 584;
			tekster_mc.y = 977.5;
			tekster_mc.mask = tekster_maske_mc;
			
		}
		
		override public function Play():void
		{
		   colour_line_mc.gotoAndPlay(1);
		   Tweener.addTween(bundt_blok_mc, {y:0, time:0.16, transition:"easeOutSine"});
		   Tweener.addTween(tekster_mc, {y:889.5, time:0.24, transition:"easeOutSine"});
		   Tweener.addTween(colour_line_mc, {alpha:1.0, time:0.04, delay:0.08, transition:"linear"});
		   Tweener.addTween(colour_line_mc, {y:540, time:0.12, delay:0.12, transition:"easeOutSine"});
		}

		override public function Stop():void
		{
			Tweener.addTween(colour_line_mc, {y:550, time:0.04, transition:"easeOutSine"});
			Tweener.addTween(colour_line_mc, {alpha:0, time:0.04, delay:0.04, transition:"linear"});
			Tweener.addTween(tekster_mc, {y:977.5, time:0.12, transition:"easeOutSine"});
			Tweener.addTween(bundt_blok_mc, {y:89, time:0.16, delay:0.04, transition:"easeOutSine"});
			setTimeout(removeTemplate, 1000);
		}
		
		override public function SetData(xmlData:XML):void 
		{
			super.SetData(xmlData);
			
			for each (var element:XML in xmlData.children())
			{
				if (element.@id == "f0")
				{
					//name_mc.f0.text = element.data.@value;
				}
				if (element.@id == "f1")
				{
					//nickname_mc.f1.text = element.data.@value;
				}
			}
			
			tekster_mc.f0.text = tekster_mc.f0.text.toUpperCase();
			//tekster_mc.f0.autoSize = TextFieldAutoSize.LEFT;
			//tekster_mc.f1.autoSize = TextFieldAutoSize.LEFT;

		}
	}
}
Last edited by tusa on 18 Oct 2017, 16:04, edited 1 time in total.

Re: Scale text to fit textfield

#4
You Need to call the ScaleDownTextField function inside your SetData. Use this code and Change the last Parameter of the ScaleDownTextField call to the Maximum size the "tekster_mc.f0" should have. If the text is becoming bigger than this value, the text will be scaled.

Code: Select all

package
{
	import flash.display.MovieClip;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextField;
	import flash.text.TextFormat;	
	import flash.utils.Timer;
	import flash.utils.setTimeout;
	import flash.events.TimerEvent;
	import flash.events.Event;
	import flash.display.Shape;
	import flash.geom.ColorTransform; 
	import se.svt.caspar.ICommunicationManager;
	import se.svt.caspar.IRegisteredDataSharer;
	import se.svt.caspar.template.CasparTemplate;
	import caurina.transitions.Tweener;

	public class KV17_bundt extends CasparTemplate
	{
		private static function ScaleDownTextField(Input:String, Field:TextField, initialFieldWidth:int):void
		{
			var wi:int = 660;
			Field.autoSize = TextFieldAutoSize.LEFT;
			Field.scaleX = 1;
			Field.text = Input;
			if (wi < Field.width)
			{
				Field.scaleX = wi / Field.width;
			}
		}
		
		override public function postInitialize():void
		{
			bundt_blok_mc.y = 89;
			bundt_blok_mc.mask = bundt_blok_maske_mc;
			colour_line_mc.alpha = 0.0;
			colour_line_mc.y = 584;
			tekster_mc.y = 977.5;
			tekster_mc.mask = tekster_maske_mc;
			
		}
		
		override public function Play():void
		{
		   colour_line_mc.gotoAndPlay(1);
		   Tweener.addTween(bundt_blok_mc, {y:0, time:0.16, transition:"easeOutSine"});
		   Tweener.addTween(tekster_mc, {y:889.5, time:0.24, transition:"easeOutSine"});
		   Tweener.addTween(colour_line_mc, {alpha:1.0, time:0.04, delay:0.08, transition:"linear"});
		   Tweener.addTween(colour_line_mc, {y:540, time:0.12, delay:0.12, transition:"easeOutSine"});
		}

		override public function Stop():void
		{
			Tweener.addTween(colour_line_mc, {y:550, time:0.04, transition:"easeOutSine"});
			Tweener.addTween(colour_line_mc, {alpha:0, time:0.04, delay:0.04, transition:"linear"});
			Tweener.addTween(tekster_mc, {y:977.5, time:0.12, transition:"easeOutSine"});
			Tweener.addTween(bundt_blok_mc, {y:89, time:0.16, delay:0.04, transition:"easeOutSine"});
			setTimeout(removeTemplate, 1000);
		}
		
		override public function SetData(xmlData:XML):void 
		{
			//super.SetData(xmlData);
			
			for each (var element:XML in xmlData.children())
			{
				if (element.@id == "f0")
				{
					ScaleDownTextField(element.data.@value.toString().toUpperCase(), tekster_mc.f0, 200);
				}
			}
			
		}
	}
}
Didi Kunz
CasparCG Client-Programmer, Template Maker & Live CG-Operator
Media Support, CH-5722 Gränichen, Switzerland http://mediasupport.ch/
Problems? Guide to posting Bug reports & Feature requests

Re: Scale text to fit textfield

#5
Thank you, that did the trick, now it works.
Brg Tue.
didikunz wrote:
18 Oct 2017, 14:25
You Need to call the ScaleDownTextField function inside your SetData. Use this code and Change the last Parameter of the ScaleDownTextField call to the Maximum size the "tekster_mc.f0" should have. If the text is becoming bigger than this value, the text will be scaled.

Code: Select all

package
{
	import flash.display.MovieClip;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextField;
	import flash.text.TextFormat;	
	import flash.utils.Timer;
	import flash.utils.setTimeout;
	import flash.events.TimerEvent;
	import flash.events.Event;
	import flash.display.Shape;
	import flash.geom.ColorTransform; 
	import se.svt.caspar.ICommunicationManager;
	import se.svt.caspar.IRegisteredDataSharer;
	import se.svt.caspar.template.CasparTemplate;
	import caurina.transitions.Tweener;

	public class KV17_bundt extends CasparTemplate
	{
		private static function ScaleDownTextField(Input:String, Field:TextField, initialFieldWidth:int):void
		{
			var wi:int = 660;
			Field.autoSize = TextFieldAutoSize.LEFT;
			Field.scaleX = 1;
			Field.text = Input;
			if (wi < Field.width)
			{
				Field.scaleX = wi / Field.width;
			}
		}
		
		override public function postInitialize():void
		{
			bundt_blok_mc.y = 89;
			bundt_blok_mc.mask = bundt_blok_maske_mc;
			colour_line_mc.alpha = 0.0;
			colour_line_mc.y = 584;
			tekster_mc.y = 977.5;
			tekster_mc.mask = tekster_maske_mc;
			
		}
		
		override public function Play():void
		{
		   colour_line_mc.gotoAndPlay(1);
		   Tweener.addTween(bundt_blok_mc, {y:0, time:0.16, transition:"easeOutSine"});
		   Tweener.addTween(tekster_mc, {y:889.5, time:0.24, transition:"easeOutSine"});
		   Tweener.addTween(colour_line_mc, {alpha:1.0, time:0.04, delay:0.08, transition:"linear"});
		   Tweener.addTween(colour_line_mc, {y:540, time:0.12, delay:0.12, transition:"easeOutSine"});
		}

		override public function Stop():void
		{
			Tweener.addTween(colour_line_mc, {y:550, time:0.04, transition:"easeOutSine"});
			Tweener.addTween(colour_line_mc, {alpha:0, time:0.04, delay:0.04, transition:"linear"});
			Tweener.addTween(tekster_mc, {y:977.5, time:0.12, transition:"easeOutSine"});
			Tweener.addTween(bundt_blok_mc, {y:89, time:0.16, delay:0.04, transition:"easeOutSine"});
			setTimeout(removeTemplate, 1000);
		}
		
		override public function SetData(xmlData:XML):void 
		{
			//super.SetData(xmlData);
			
			for each (var element:XML in xmlData.children())
			{
				if (element.@id == "f0")
				{
					ScaleDownTextField(element.data.@value.toString().toUpperCase(), tekster_mc.f0, 200);
				}
			}
			
		}
	}
}