Flash - can't get TextLayoutFormat of TLFTextField

2.3k Views Asked by At

All I want to do is get the formatting properties of a TLFTextField and apply it to another TLFTextField. This was simple using the classic TextField:

var textFormat:TextFormat = text1.getTextFormat();
text2.setTextFormat(textFormat);

TLFTextField has a getTextFormat and setTextFormat function, but they are both very buggy. getTextFormat only works if you change the selectable property to true, otherwise it generates a null object error. setTextFormat generates a NaN error when some of the properties of the TextFormat object are not null.

The TextLayoutFormat object is supposed to be used instead for TLFTextFields. You set the object by doing the following:

var text1:TLFTextField = new TLFTextField();
var textLayoutFormat:TextLayoutFormat = new TextLayoutFormat();
var textFlow:TextFlow = text1.textFlow;
textFlow.hostFormat = textLayoutFormat;
textFlow.flowComposer.updateAllControllers();

However, I cannot figure out how to 'get' the TextLayoutFormat from text1 now. One person suggested the following:

textLayoutFormat = ((text1.textFlow.getChildAt(0) as ParagraphElement).getChildAt(0) as SpanElement).computedFormat as TextLayoutFormat;

But this just returned null. Does anyone know how to get the TextLayoutFormat so I can apply it to another TLFTextField?

2

There are 2 best solutions below

2
On

Are you using Flex or Flash? I've recently had my own headaches with TLF. This page was a good reference for me: http://flashthusiast.com/2010/05/05/getting-started-with-the-tlftextfield-class-in-actionscript-3-0-and-flash-cs5/ Are you setting the text format prior to the text? I've done that and haven't had your null problem.

0
On

In simple case this works

var format:TextLayoutFormat = textField.textFlow.hostFormat as TextLayoutFormat;
format.fontFamily = fontFamily;
textField.textFlow.hostFormat = format;
textField.textFlow.flowComposer.updateAllControllers();