If you are using RichEditableText component in Flex 4 for text layout and want to call actionscript methods within the texts, the old Flex 3 event handler “TextEvent.LINK” won’t work as it is using Text Layout Framework(TLF) now…
There are 2 ways of handling AS methods within RichEditableText component:
1. Use same old Flex 3 “event:myLinkEvent” in HTML “a” tag and add an event listener to RichEditableText.textFlow instead, it will return “myLinkEvent” as event type from example.
2. Add an event handler function to new “click” attribute in HTML “a” tag, it will always return “click” as event type.
Here is the complete example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | <?xml version="1.0" encoding="utf-8"?> <s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" xmlns:s="library://ns.adobe.com/flex/spark" xmlns:mx="library://ns.adobe.com/flex/mx" creationComplete="init()" width="500" height="400"> <fx:Script> <![CDATA[ import flashx.textLayout.events.FlowElementMouseEvent; protected function init():void { myTxt.textFlow.addEventListener("myLinkEvent", onTextLink); } protected function onTextLink(e:FlowElementMouseEvent):void { myOutput.text = "Type: "+e.type; } ]]> </fx:Script> <s:RichEditableText id="myTxt" editable="false" x="100" y="100"> <s:content> <s:a href="http://www.google.com">Test link 1: open url</s:a><s:br/> <s:a href="event:myLinkEvent">Test link 2: call AS method with "myLinkEvent" type</s:a><s:br/> <s:a click="onTextLink(event)">Test link 3: call AS method with "click" type</s:a> </s:content> </s:RichEditableText> <s:Label id="myOutput" x="100" y="150" /> </s:Application> |
Note that you need to set RichEditableText attribute “editable” to “false” in order for link to work.
5 users commented in " Flex 4 – RichEditableText(TLF) Link example "
Follow-up comment rss or Leave a TrackbackI cant get this working for dynamically loaded html, your example works but the following does not for me..
It seems to have chewed the code in my previous comment. I’m wondering if I need to wait for an event before setting the listener on dynamic as3 generated textflow text ? I dont seem to see any applicable in the class signature.
Unfortunately I don’t think it will work with any dynamic imported or static html format since it only works in text layout format (Flex 4 specified)…
This might help. It’s working for me with anchors imported at runtime from externalized XML:
http://www.websector.de/blog/2010/02/25/quick-tip-flex-4-using-asfunction-in-tlf-text-layout-framework/
if imports as html, “href=”event:myLinkEvent” will work but need to make sure using RichEditableText component instead of RichText and set “editable” and “selectable” attributes to false.
Leave A Reply