Trying to autosize text

#1
Hi everyone.

I'm using a Dynamic Text on my Caspar template, but when I put a large text, doesn't show everything.

So I'm trying to create a autoSize function. Searching the forum I've found the following code:

Code: Select all

package{

	import flash.display.MovieClip;
	import se.svt.caspar.ICommunicationManager;
	import se.svt.caspar.IRegisteredDataSharer;
	import se.svt.caspar.template.CasparTemplate;

	public function resizeText():void
      {
         if (Texts.f0.width > 490)
         {
            
            newSize.size--;
            newSize.letterSpacing = newSize.letterSpacing - 0.2;
            txtTestFormat.letterSpacing = newSize.letterSpacing;
            txtTestFormat.size = newSize.size;
            Texts.f0.setTextFormat(txtTestFormat);
            resizeText();
         }
         else
         {
            Texts.f0.autoSize = TextFieldAutoSize.RIGHT;
            Texts.f0.width = 500;
            //Texts.f0.setTextFormat(txtTestFormat);
         }
         if (Texts.f1.width > 490)
         {
            
            newSize.size--;
            newSize.letterSpacing = newSize.letterSpacing - 0.2;
            txtTestFormat.letterSpacing = newSize.letterSpacing;
            txtTestFormat.size = newSize.size;
            Texts.f1.setTextFormat(txtTestFormat);
            resizeText();
         }
         else
         {
            Texts.f1.autoSize = TextFieldAutoSize.LEFT;
            Texts.f1.width = 500;
            //Texts.f0.setTextFormat(txtTestFormat);
         }
		 
      }
	  resizeText();
}
I have 2 Dynamic Texts, f0 and f1, if I try to call the function on the fla file I get an error compiling, so I'm trying to call the function inside the .as file, but it didn't worked.

Any solution? I've tried downloading an example I found on the forum too the file names are "Schneebericht" but the texts there are being pulled on a XML inside the .as so I think I cannot use that.

Regards! :)

Re: Trying to autosize text

#2
You can use the "Schneebericht" example, the XML it's the way, the data comes to a template from the client. There is also a tutorial about "dynamic images" in the wiki where the whole process is explaned also. It's all about overwriting SetData() inside you template.
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: Trying to autosize text

#3
Hi didikunz,

Thanks for explaining some things for me, I've tried creating my script but I'm getting those errors on Console.

Code: Select all

[2017-01-10 13:45:30.051] [1288] [info]    Received message from 127.0.0.1: CG 1-20 ADD 1 "CASTER-AUTOSIZE" 1 "<templateData><componentData id=\"f0\"><data id=\"text\" value=\"Teste 1\"/></componentData><componentData id=\"f1\"><data id=\"text\" value=\"Teste 2\"/></componentData></templateData>"\r\n
[2017-01-10 13:45:30.052] [8544] [info]    flash[cg20.fth.720p5994|0] Initialized.
[2017-01-10 13:45:30.053] [8544] [info]    flash[cg20.fth.720p5994|0] Invoking add-command: <invoke name="Add" returntype="xml"><arguments><number>1</number><string>CASTER-AUTOSIZE.ft</string><true/><string></string><string><![CDATA[ <templateData><componentData id="f0"><data id="text" value="Teste 1"/></componentData><componentData id="f1"><data id="text" value="Teste 2"/></componentData></templateData>]]></string></arguments></invoke>
Vector smash protection is enabled.
[2017-01-10 13:45:30.121] [7784] [debug]   flash-player[cg20.fth.720p5994|1280x720] [command]      <invoke name="OnCommand" returntype="xml"><arguments><string>Command recieved @GetInfo@?@</string></arguments></invoke>
[2017-01-10 13:45:30.142] [7784] [info]    flash-player[cg20.fth.720p5994|1280x720] Initialized.
[2017-01-10 13:45:30.143] [7784] [debug]   flash-player[cg20.fth.720p5994|1280x720] [command]      <invoke name="OnCommand" returntype="xml"><arguments><string>Command recieved @Add@1@</string></arguments></invoke>
[2017-01-10 13:45:30.406] [7784] [error]   flash-player[cg20.fth.720p5994|1280x720] [error]        <invoke name="OnError" returntype="xml"><arguments><string>@Add@1@TypeError: Error #1009@</string></arguments></invoke>
[2017-01-10 13:45:30.407] [7784] [error]   flash-player[cg20.fth.720p5994|1280x720] [error]        <invoke name="OnError" returntype="xml"><arguments><string>@Play@1@ReferenceError: No template queued on layer 1@</string></arguments></invoke>
[2017-01-10 13:45:30.479] [7784] [info]    flash-player[cg20.fth.720p5994|1280x720] Uninitialized.
[2017-01-10 13:45:30.480] [8544] [debug]   Executed command: CGCommand
[2017-01-10 13:45:30.480] [8544] [info]    Sent message to 127.0.0.1: 202 CG OK\r\n
My .as file is

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.events.TimerEvent;
    import flash.events.Event;
	import se.svt.caspar.ICommunicationManager;
	import se.svt.caspar.IRegisteredDataSharer;
	import se.svt.caspar.template.CasparTemplate;
	import caurina.transitions.Tweener;
	
	public class Funcionando extends CasparTemplate
	{
		
		private const customParameterDescription:XML = 	
		<parameters>
	   		<parameter id="f0" type="string" info="Caster Esquerda" />
	   		<parameter id="f1" type="string" info="Caster Direita" />
		</parameters>;
		
		private var init:Boolean = false;
		public var TextosFade:MovieClip;
		
		private static function ScaleDownTextFieldLeft(Input:String, Field:TextField):void
		{
			var wi:int = Field.width;
		    Field.autoSize = TextFieldAutoSize.LEFT;
		    Field.text = Input;
			if (wi < Field.width)
			{
		    	Field.scaleX = wi / Field.width;
			}
		}
		private static function ScaleDownTextFieldRight(Input:String, Field:TextField):void
		{
			var wi:int = Field.width;
		    Field.autoSize = TextFieldAutoSize.RIGHT;
		    Field.text = Input;
			if (wi < Field.width)
			{
		    	Field.scaleX = wi / Field.width;
			}
		}
			
		override public function SetData(xmlData:XML):void 
		{			

		   for each (var element:XML in xmlData.children())
		   {
			  if (element.@id == "f0") 		
			  {
				  ScaleDownTextFieldRight(element.data.@value, TextosFade.f0);
			  }
			  if (element.@id == "f1")
			  {
				  ScaleDownTextFieldLeft(element.data.@value, TextosFade.f1);
			  }
		   }
		   		   
		   super.SetData(xmlData);		

		}
	}
}
Already tried with super.SetData commented too.

My texts are f0 and f1, they are inside a MovieClip called TextosFade, I'm also using a Classic Tween to make a FadeIn, FadeOut animation.

Regards! :)

Re: Trying to autosize text

#4
You define a variable in code called "TextosFade". I guess you have a movieclip of the same name in your .fla file. When you try to set your "TextosFade.f0" dynamic field, it does not find the field f0 inside your variable. Just get rid of the line: "public var TextosFade:MovieClip;" so that it finds the dynamic textfield inside your movieclip.
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

Who is online

Users browsing this forum: No registered users and 1 guest

cron