Detecting when text has run into the end of a container in a TextFlow object
Actionscript, Flex Add commentsMore fun and games with the Text Layout Framework. So, I have a load of links being flowed one by one through a bunch of linked containers. I need to detect when a link reaches the end and has ‘overflowed’ so to speak.
To do this you need to make sure your ContainerController objects have their ScrollPolicy set to OFF so the flowComposer recognises that the text is not in the container anymore. This will not halt the flow however so you need to add a nice bit of code that will detect if the FlowElement is no longer within a container.
Here’s the code …
Detecting when text has run into the end of the ContainerController in a TextFlow object
- /*
- A good idea to set a config object to pass into the TextFlow object when you create it
- */
- config = Configuration(TextFlow.defaultConfiguration).clone();
- /*
- And add this overflowPolicy to it as well as your formatting defaults
- */
- config.overflowPolicy = OverflowPolicy.FIT_DESCENDERS;
- textFlow = new TextFlow(config);
- /*
- Create your containers from sprites and make sure the scrollPolicy is OFF
- */
- var controller:ContainerController = new ContainerController(containerSprite,width,height);
- controller.verticalScrollPolicy = controller.horizontalScrollPolicy = ScrollPolicy.OFF;
- textFlow.flowComposer.addController(controller);
- /*
- Now imagine we are in a loop which adds a paragraph of lorem ipsum to the textflow and updates the controllers and checks if the flow is now out of the bounds of the container
- */
- while(LOOP)
- {
- textFlow.addChild(myParagraphElement); // you've created the paragraph
- textFlow.flowComposer.updateAllControllers(); // you've created the controllers
- var containerIndex:int = textFlow.flowComposer.findControllerIndexAtPosition(textFlow.textLength-1);
- if(containerIndex==-1) textFlowCompleteHandler();
- }
It’s these two lines that do the detection …
-
-
var containerIndex:int = textFlow.flowComposer.findControllerIndexAtPosition(textFlow.textLength-1);
-
-
if(containerIndex==-1) textFlowCompleteHandler();
The findControllerIndexAtPosition method will return -1 if the character index passed in is not within a container. Remember to use textFlow.textLength-1 to get the last character. The textFlow variable is an instance of the TextFlow object.
The textFlowCompleteHandler can be any method that stops the insertion of FlowElement objects happening.


April 23rd, 2010 at 4:06 pm
Hey, thanks for the post. I didn’t realize you could set scroll policies on controllers until reading this so I appreciate it.
April 23rd, 2010 at 4:09 pm
I’m not entirely sure how it would work if you did set scroll bars for linked containers.
July 2nd, 2010 at 8:09 pm
thanks for this. the documentation for TLF is a cruel mistress. one thing to note, however, is that the OverflowPolicy.FIT_DESCENDERS is the default OverflowPolicy, so setting it is only required if switching from a previously set OverflowPolicy.FIT_ANY constant.