Splitting text and spacing properly in AS3 template

#1
Hi Everyone

I've been asked to create an FT that I cannot figure out.
I would normally send a persons name as a single variable, lets call that variable f1.

I'd then use this function to make the font fit into the space provided

Code: Select all

private static function ScaleDownTextField(Input:String, Field:TextField):void
		{
			var wi:int = Field.width;
			var xcoord: int = Field.x;
		    Field.autoSize = TextFieldAutoSize.CENTER;
		    Field.text = Input;
			if (wi < Field.width)
			{
		    	Field.scaleX = wi / Field.width;
				Field.x = xcoord;
			}
		}


followed by this to apply the function to the text box:-
override public function SetData(xmlData:XML):void
{

for each (var element:XML in xmlData.children())
{ if (element.@id == "f1")
{
ScaleDownTextField(element.data.@value, Text1.f1);

}
}
}

What I need to do is take the Forename and Surname and split them so that they can be a different weighted and sized font.
I think I can split the variable based upon the space, I think I can then create a new text box for the surname at the physical end of the forename and set its font, I just cant get my head round how and I'm having a but of a brain freeze.

Can anyone help?

Re: Splitting text and spacing properly in AS3 template

#2
I would always use two varables for first and last name, over spliting on a space. For instance: Where to split: "Jose Maria da Silva"? The trick is to use two very small textfields like so:
Grab1.png
Grab1.png (5.58 KiB) Viewed 340 times
and code along these lines:

Code: Select all

private function ScaleDownMultiTextFields(Input1:String, Input2:String, Field1:TextField, Field2:TextField, maximalWidth:int, gap:int, positionX: int):void
{
	var scaleFact:Number = 1;
	
	Field1.autoSize = TextFieldAutoSize.LEFT;
	Field1.scaleX = 1;
	
	Field2.autoSize = TextFieldAutoSize.LEFT;
	Field2.scaleX = 1;
	
	Field1.text = Input1;
	Field2.text = Input2;

	var totalWitdh:int = Field1.width + Field2.width + gap;
	if (maximalWidth < totalWitdh)
	{
		scaleFact = maximalWidth / totalWitdh;
		Field1.scaleX = scaleFact;
		Field2.scaleX = scaleFact;
	}
	//centered
	Field1.x = positionX + ((maximalWidth - totalWitdh) / 2);
	Field2.x = Field1.x + Field1.width + (gap * scaleFact);
}
It sets the TextFields AutoSize property before adding the text to it. Because the fields are so small they will expand to fit the text in any case. That's how you get a reliable Width to calculate the offsets. The code here centers the two fields in a given space, defined by positionX and maximalWidth. That can easy be changed to be left or right aligned.
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