0

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 Answers2

0

In simple case this works

var format:TextLayoutFormat = textField.textFlow.hostFormat as TextLayoutFormat;
format.fontFamily = fontFamily;
textField.textFlow.hostFormat = format;
textField.textFlow.flowComposer.updateAllControllers();
average dev
  • 1,128
  • 7
  • 22
0

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.

ChickensDontClap
  • 1,871
  • 4
  • 22
  • 39
  • I'm using FlashDevelop which uses the Flex SDK. Yes I am setting the text format before I set the text. I've had a look over that article, and they use a TextFormat object instead of TextLayoutFormat. setTextFormat just doesn't seem to like it when some properties of the TextFormat parameter are not null, eg linespacing. – Ashley Muller Nov 06 '11 at 23:59
  • ok when I run getTextFormat, the property 'leading' of the returned TextFormat is a weird negative number, looks like min int, and the property 'letterSpacing' is NaN. These cause setTextFormat to crash. If I set these properties to null before running setTextFormat, all is ok, but obviously its not getting the leading and letterspacing which I need. I think I'll open a new question for this as it's a different problem. – Ashley Muller Nov 07 '11 at 00:23