you can also use the Text Layout Framework with E4X for a bit more flexibility. the following code sets up a TextLayout object using format and container attributes from an XML file and content from a plain text file, but it shows how to incorporate E4X with TLF so you should be able to easily tailor it to your needs.
caller:
[SWF(width="1000", height="600", frameRate="60", backgroundColor="#330000")]
var layout:XMLTextLayout = new XMLTextLayout("XMLTextLayout.XML");
addChild(layout);
XMLTextLayout class:
package
{
import flash.display.Sprite;
import flash.net.URLLoader;
import flash.net.URLRequest;
import flash.events.Event;
import flash.events.IOErrorEvent;
import flashx.textLayout.container.ContainerController;
import flashx.textLayout.formats.TextLayoutFormat;
import flashx.textLayout.elements.Configuration;
import flashx.textLayout.elements.TextFlow;
import flashx.textLayout.edit.SelectionManager;
import flashx.textLayout.edit.SelectionFormat;
import flashx.textLayout.edit.EditingMode;
import flashx.textLayout.conversion.TextConverter;
import flashx.textLayout.conversion.ConversionType;
public class XMLTextLayout extends Sprite
{
//Class Variables
private var xmlData:XML;
//Constructor
public function XMLTextLayout(XMLFileURL:String)
{
var xmlLoader:URLLoader = new URLLoader();
xmlLoader.addEventListener(IOErrorEvent.IO_ERROR, errorHandler);
xmlLoader.addEventListener(Event.COMPLETE, xmlDataHandler);
xmlLoader.load(new URLRequest(XMLFileURL));
}
private function xmlDataHandler(evt:Event):void
{
evt.target.removeEventListener(IOErrorEvent.IO_ERROR, errorHandler);
evt.target.removeEventListener(Event.COMPLETE, xmlDataHandler);
xmlData = new XML(evt.target.data);
var textLoader:URLLoader = new URLLoader();
textLoader.addEventListener(IOErrorEvent.IO_ERROR, errorHandler);
textLoader.addEventListener(Event.COMPLETE, initiateLayout);
textLoader.load(new URLRequest(xmlData.@textFileURL));
}
private function errorHandler(evt:IOErrorEvent):void
{
throw(evt.text);
}
//Layout Text
private function initiateLayout(evt:Event):void
{
evt.target.removeEventListener(IOErrorEvent.IO_ERROR, errorHandler);
evt.target.removeEventListener(Event.COMPLETE, initiateLayout);
var textFormat:TextLayoutFormat = new TextLayoutFormat();
for each (var formatProperty:XML in xmlData.format.*)
{
if (textFormat.hasOwnProperty(formatProperty.name()))
textFormat[formatProperty.name()] = formatProperty.toString();
}
var configuration:Configuration = new Configuration();
configuration.textFlowInitialFormat = textFormat;
configuration.focusedSelectionFormat = new SelectionFormat(xmlData.format.@highlightColor);
var textFlow:TextFlow = TextConverter.importToFlow(evt.target.data, xmlData.@conversionType, configuration);
for each (var element:XML in xmlData..container)
{
var sprite:Sprite = new Sprite();
sprite.graphics.drawRect(0, 0, element.width, element.height);
sprite.x = element.x;
sprite.y = element.y;
addChild(sprite);
textFlow.flowComposer.addController(new ContainerController(sprite, sprite.width, sprite.height));
}
textFlow.flowComposer.updateAllControllers();
textFlow.interactionManager = new SelectionManager();
textFlow.interactionManager.editingMode == EditingMode.READ_SELECT;
}
}
}
XML file:
<?XML version="1.0" encoding="UTF-8"?>
<textLayout textFileURL="Lorem Ipsum.txt" conversionType="plainTextFormat">
<format highlightColor="0xFF0000">
<color>0xAA0000</color>
<fontSize>10.5</fontSize>
<fontWeight>bold</fontWeight>
<textAlign>left</textAlign>
<verticalAlign>middle</verticalAlign>
<columnCount>4</columnCount>
<paddingLeft>40</paddingLeft>
<paddingRight>40</paddingRight>
<paddingTop>40</paddingTop>
<paddingBottom>40</paddingBottom>
</format>
<container>
<x>0</x>
<y>0</y>
<width>500</width>
<height>600</height>
</container>
<container>
<x>500</x>
<y>70</y>
<width>500</width>
<height>480</height>
</container>
</textLayout>
<!--
Notes:
1. Format element names must match the public property names of the TextLayoutFormat class.
2. ConversionType and format element values must match the string value of the property's public constant.
-->