Skip to main content
Inspiring
May 20, 2010
Answered

Loop trough TextLine children

  • May 20, 2010
  • 1 reply
  • 560 views

Hi,

How can I loop through the textline children to examine which "child" is a character or an inline graphic element?

tks.

This topic has been closed for replies.
Correct answer robin_briggs

Might help to know what you need this for, since there are different ways to go about it. But, for some background...

A TextLine draws itself, which includes all the text. A TextLine created by TLF may have as children inline graphics, underline, and strikethrough adornments. The TextLine has a flag, hasGraphicElements, which will tell you whether it contains inline graphics. If it does, and if you have access to the TextFlow at this point, the easiest way to find the inlines might be to get the text range of the line, and then search the TextFlow over this range (using findLeaf to get the first leaf, and then iterating through using nextLeaf). If you find a leaf that is an InlineGraphicElement, you can get the inline from the element. You can get the text range of the line by doing something like this:

     var textFlowLine:TextFlowLine = textLine.userData as TextFlowLine;

     var leaf:FlowLeafElement = textFlow.findLeafAtPosition(textFlowLine.absoluteStart);

     var pos:int = textFlowLine.absoluteStart;

     while (pos < textFlowLine.absoluteStart + textFlowLine.textLength)

     {

          if (leaf is InlineGraphicElement)

               displayObject = InlineGraphicElement(leaf).graphic;

          pos += leaf.textLength;

          leaf = leaf.getNextLeaf();

     }

I haven't compiled or run this code, so YMMV, but something like this should work.

Alternatively, the TextLine is a DisplayObjectContainer, and you could search its hierarchy. This is easy if the you know which DisplayObjects might be inlines, but more difficult otherwise. You should know that the inline won't be a direct child of the TextLine. Instead, when the player attaches the inline graphic to the TextLine it uses a Sprite to control the placement, and alpha properties of the inline, so the actual inline graphic would be a "grandchild" of the TextLine.

Hope this helps,

- robin

1 reply

robin_briggsCorrect answer
Adobe Employee
May 21, 2010

Might help to know what you need this for, since there are different ways to go about it. But, for some background...

A TextLine draws itself, which includes all the text. A TextLine created by TLF may have as children inline graphics, underline, and strikethrough adornments. The TextLine has a flag, hasGraphicElements, which will tell you whether it contains inline graphics. If it does, and if you have access to the TextFlow at this point, the easiest way to find the inlines might be to get the text range of the line, and then search the TextFlow over this range (using findLeaf to get the first leaf, and then iterating through using nextLeaf). If you find a leaf that is an InlineGraphicElement, you can get the inline from the element. You can get the text range of the line by doing something like this:

     var textFlowLine:TextFlowLine = textLine.userData as TextFlowLine;

     var leaf:FlowLeafElement = textFlow.findLeafAtPosition(textFlowLine.absoluteStart);

     var pos:int = textFlowLine.absoluteStart;

     while (pos < textFlowLine.absoluteStart + textFlowLine.textLength)

     {

          if (leaf is InlineGraphicElement)

               displayObject = InlineGraphicElement(leaf).graphic;

          pos += leaf.textLength;

          leaf = leaf.getNextLeaf();

     }

I haven't compiled or run this code, so YMMV, but something like this should work.

Alternatively, the TextLine is a DisplayObjectContainer, and you could search its hierarchy. This is easy if the you know which DisplayObjects might be inlines, but more difficult otherwise. You should know that the inline won't be a direct child of the TextLine. Instead, when the player attaches the inline graphic to the TextLine it uses a Sprite to control the placement, and alpha properties of the inline, so the actual inline graphic would be a "grandchild" of the TextLine.

Hope this helps,

- robin