Mar 23, 2010 3:58 AM
How can I detect where an element sits within the flow?
-
Like (0)
I know I can get the position as a number of characters in from the root but specifically I need to detect exactly where my element sits within the line it is on and in particular if a LINK element object is sat at the start of the line and is not broken.
I know you can set the breakOpportunity property to prevent a link from breaking on a line so can I detect if a link has broken?
Surely there's a way to detect where an element or character of an element sits on a line.
Any help on this would be appreciated. I've been fighting this framework for the last 4 days now ... I need this to work!
:-)
Start with the LinkElement and its owning TextFlow.
var textFlow:TextFlow
var link:LinkElement;
// start and end of the link
var linkStartPos:int = link.getAbsoluteStart();
var linkEndPos:int = linkStartPos+link.textLength;
var textFlowLine:TextFlowLine = textFlow.flowComposer.findLineAtPosition(linkStart);
The textFlowLine has a absoluteStart and a textLength property. Compare then with the link start/stop pos to see where the link falls on the line
The link may start on this line and end on a following line.
One caveat - when things are out of view TLF will stop composing early. To force all content to be fully composed and broken into lines use:
textFlow.flowComposer.composeToPosition(textFlow.textLength)
Hope that helps,
Richard
Hey thanks for this. Sounds promising.
The only thing I can think of is that I will need to add the link to the flow first and then if it is on a new line and at the start then I will have to immediately remove it from the flow, add a new paragraph element object and then add the link as a child of the paragraph. Unless I can add the paragraph and then re-parent the link without having to remove it first.
Sounds better than my technique at the moment which is using a fixed width font and trying to calculate when the link hits a new line.
Thanks.
it throws an error here : var textFlowLine:TextFlowLine = textFlow.flowComposer.findLineAtPosition(linkStart);
textFlowLine comes back as null. I'm using a standard TextArea component at the moment to render the flow. Does this suggest that there isn't a flowComposer object instantiated?
I'm going through a huge array of names and composing the textFlow link by link.
Before I attempt to work out the line position and link position etc I am adding the link to the flow and also running : textFlow.flowComposer.composeToPosition(textFlow.textLength);
It's almost as if the flow hasn't been composed. Do I need to setup a listener or something to detect when it is ready?
var link:LinkElement = new LinkElement();
link.href = "event:"+o.id;
var linkText:SpanElement = new SpanElement();
linkText.text = o.first_name+" "+o.last_name;
var space:SpanElement = new SpanElement();
space.text = " ";
link.addChild(linkText);
para.addChild(link);
textFlow.flowComposer.composeToPosition(textFlow.textLength);
var linkStart:int = link.getAbsoluteStart();
var linkEnd:int = linkStart+link.textLength;
var textFlowLine:TextFlowLine = textFlow.flowComposer.findLineAtPosition(linkStart);
var lineStart:int = textFlowLine.absoluteStart;
var isStartOfLine:Boolean = linkStart==lineStart;
so I added a CompositioncompleteEvent listener to the textFlow object and that worked ... for two of the links. Then it just stopped listening.
Still trying to solve this. just need to detect when the flow has updated. I am getting good information about the position of the link within the line but just can't detect when the flow has updated as I add each link one by one.
I'm able to update my flow link by link using a simple Timer object. This is less than ideal though.
I'm not sure what would cause the events to stop coming, and as long as your listener is still attached you should still be getting more events. I think I will need more information. What changes are happening where you expect to see events but don't? Have you tried attaching a listener for the UpdateCompleteEvent?
- robin
Notice in my code I'm using a Timer interval to create each LinkElement object and add it to the Flow. Creates a nice animation as the flow is created (which is actually what I wanted so bonus).
Are you adding a whole set of links, and then calling updateAllControllers(), or do you add a single link at a time, updating after that? Are all the links visible on screen?
If you add all the links at once, then you would only get one event. If the links are not visible, they may not cause update events when they get added.
- robin
I'm adding the link one at a time, in the example given it's a TextArea component that is the host. I was using the composeToPosition method.
I wish we were in the same timezone.
I wonder if something happened that caused Flex to change the textFlow that it had, and use a different one instead. Then you'd be attached to the old one and not the new one, and wouldn't get the events that go to the new one.
North America
Europe, Middle East and Africa
Asia Pacific
Copyright © 2011 Adobe Systems Incorporated. All rights reserved.
Use of this website signifies your agreement to the Terms of Use and Online Privacy Policy (updated 07-14-2009).