The following example shows how you can set the text alignment and text direction in a Spark TextArea control in Flex 4 by setting the textAlign and direction styles.
Full code after the jump.
The following example(s) require Flash Player 10 and the Adobe Flex 4 SDK. To download the Adobe Flash Builder 4 beta, check out the Adobe Flash Builder 4 page on the Adobe Labs site. To download the latest build of the Flex 4 SDK, see http://opensource.adobe.com/wiki/display/flexsdk/Download+Flex+4. For instructions on using the beta Flex 4 SDK in Flex Builder 3, see "Using the beta Flex 4 SDK in Flex Builder 3".
<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2008/10/22/setting-text-alignment-on-an-fxtextarea-control-in-flex-gumbo/ -->
<Application name="FxTextArea_textAlign_test"
xmlns="http://ns.adobe.com/mxml/2009"
layout="vertical"
verticalAlign="middle"
backgroundColor="white">
<Script>
<![CDATA[
import flashx.textLayout.formats.Direction;
import flashx.textLayout.formats.TextAlign;
]]>
</Script>
<Declarations>
<Array id="textAlignDP">
<String>{TextAlign.START}</String>
<String>{TextAlign.LEFT}</String>
<String>{TextAlign.CENTER}</String>
<String>{TextAlign.RIGHT}</String>
<String>{TextAlign.END}</String>
<String>{TextAlign.JUSTIFY}</String>
</Array>
<Array id="directionDP">
<String>{Direction.LTR}</String>
<String>{Direction.RTL}</String>
</Array>
<String id="lorem" source="data/lorem.html" />
</Declarations>
<ApplicationControlBar dock="true">
<Form styleName="plain">
<FormItem label="textAlign:">
<ComboBox id="textAlignComboBox"
dataProvider="{textAlignDP}" />
</FormItem>
<FormItem label="direction:">
<ComboBox id="directionComboBox"
dataProvider="{directionDP}" />
</FormItem>
</Form>
</ApplicationControlBar>
<FxTextArea id="fxTextArea"
content="{lorem}"
textAlign="{textAlignComboBox.selectedItem}"
direction="{directionComboBox.selectedItem}"
widthInChars="80"
heightInLines="25"
marginTop="20" />
</Application>
View source is enabled in the following example.
You can also set the textAlign and direction styles in an external .CSS file or <mx:Style /> block, as seen in the following example:
<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2008/10/22/setting-text-alignment-on-an-fxtextarea-control-in-flex-gumbo/ -->
<Application name="FxTextArea_textAlign_test"
xmlns="http://ns.adobe.com/mxml/2009"
layout="vertical"
verticalAlign="middle"
backgroundColor="white">
<Style>
FxTextArea {
textAlign: "justify";
direction: "rtl";
marginTop: 20;
}
</Style>
<Declarations>
<String id="lorem" source="data/lorem.html" />
</Declarations>
<FxTextArea id="fxTextArea"
content="{lorem}"
widthInChars="80"
heightInLines="25" />
</Application>
Or, you can set the textAlign and direction styles using ActionScript, as seen in the following example:
<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2008/10/22/setting-text-alignment-on-an-fxtextarea-control-in-flex-gumbo/ -->
<Application name="FxTextArea_textAlign_test"
xmlns="http://ns.adobe.com/mxml/2009"
layout="vertical"
verticalAlign="middle"
backgroundColor="white">
<Script>
<![CDATA[
import flashx.textLayout.formats.Direction;
import flashx.textLayout.formats.TextAlign;
import mx.events.ListEvent;
private function textAlignComboBox_change(evt:ListEvent):void {
var value:String = evt.currentTarget.selectedItem;
fxTextArea.setStyle("textAlign", value);
}
private function directionComboBox_change(evt:ListEvent):void {
var value:String = evt.currentTarget.selectedItem;
fxTextArea.setStyle("direction", value);
}
]]>
</Script>
<Declarations>
<Array id="textAlignDP">
<String>{TextAlign.START}</String>
<String>{TextAlign.LEFT}</String>
<String>{TextAlign.CENTER}</String>
<String>{TextAlign.RIGHT}</String>
<String>{TextAlign.END}</String>
<String>{TextAlign.JUSTIFY}</String>
</Array>
<Array id="directionDP">
<String>{Direction.LTR}</String>
<String>{Direction.RTL}</String>
</Array>
<String id="lorem" source="data/lorem.html" />
</Declarations>
<ApplicationControlBar dock="true">
<Form styleName="plain">
<FormItem label="textAlign:">
<ComboBox id="textAlignComboBox"
dataProvider="{textAlignDP}"
change="textAlignComboBox_change(event);" />
</FormItem>
<FormItem label="direction:">
<ComboBox id="directionComboBox"
dataProvider="{directionDP}"
change="textAlignComboBox_change(event)" />
</FormItem>
</Form>
</ApplicationControlBar>
<FxTextArea id="fxTextArea"
content="{lorem}"
widthInChars="80"
heightInLines="25"
marginTop="20" />
</Application>
Due to popular demand, here is the “same” example in a more ActionScript friendly format:
<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2008/10/22/setting-text-alignment-on-an-fxtextarea-control-in-flex-gumbo/ -->
<Application name="FxTextArea_textAlign_test"
xmlns="http://ns.adobe.com/mxml/2009"
layout="vertical"
verticalAlign="middle"
backgroundColor="white"
initialize="init();">
<Declarations>
<String id="lorem" source="data/lorem.html" />
</Declarations>
<Script>
<![CDATA[
import flashx.textLayout.formats.Direction;
import flashx.textLayout.formats.TextAlign;
import mx.events.ListEvent;
import mx.controls.ComboBox;
import mx.components.FxTextArea;
import mx.containers.Form;
import mx.containers.FormItem;
import mx.containers.ApplicationControlBar;
private var textAlignDP:Array;
private var directionDP:Array;
private var textAlignComboBox:ComboBox;
private var directionComboBox:ComboBox;
private var fxTextArea:FxTextArea;
private function init():void {
textAlignDP = [];
textAlignDP.push(TextAlign.START);
textAlignDP.push(TextAlign.LEFT);
textAlignDP.push(TextAlign.CENTER);
textAlignDP.push(TextAlign.RIGHT);
textAlignDP.push(TextAlign.END);
textAlignDP.push(TextAlign.JUSTIFY);
directionDP = [];
directionDP.push(Direction.LTR);
directionDP.push(Direction.RTL);
textAlignComboBox = new ComboBox();
textAlignComboBox.dataProvider = textAlignDP;
textAlignComboBox.addEventListener(ListEvent.CHANGE,
textAlignComboBox_change);
directionComboBox = new ComboBox();
directionComboBox.dataProvider = directionDP;
directionComboBox.addEventListener(ListEvent.CHANGE,
directionComboBox_change);
var formItem1:FormItem = new FormItem();
formItem1.label = "textAlign:";
formItem1.addChild(textAlignComboBox);
var formItem2:FormItem = new FormItem();
formItem2.label = "direction:";
formItem2.addChild(directionComboBox);
var form:Form = new Form();
form.styleName = "plain";
form.addChild(formItem1);
form.addChild(formItem2);
var appControlBar:ApplicationControlBar;
appControlBar = new ApplicationControlBar();
appControlBar.dock = true;
appControlBar.addChild(form);
addChildAt(appControlBar, 0);
fxTextArea = new FxTextArea();
fxTextArea.content = lorem;
fxTextArea.widthInChars = 80;
fxTextArea.heightInLines = 25;
fxTextArea.setStyle("marginTop", 20);
addChild(fxTextArea);
}
private function textAlignComboBox_change(evt:ListEvent):void {
var value:String = evt.currentTarget.selectedItem;
fxTextArea.setStyle("textAlign", value);
}
private function directionComboBox_change(evt:ListEvent):void {
var value:String = evt.currentTarget.selectedItem;
fxTextArea.setStyle("direction", value);
}
]]>
</Script>
</Application>
This entry is based on a beta version of the Flex 4 SDK and therefore is very likely to change as development of the Flex SDK continues. The API can (and will) change causing examples to possibly not compile in newer versions of the Flex 4 SDK.

{ 6 comments… read them below or add one }
Thanks to helpful examples! It really helps me understanding new text engine.
could you show me how to insert image into new textarea surrounded by texts,
i mean around image~
Thanks in advance.
Hello, Chuka~
I’m looking method to insert image into texts too.
I have decoded http://www.buzzword.com and studied it for two days, its so
Do you want to have a talk with me?
Email: shineandsky@gmail.com
mani.
hi,
Its a good axample can u pls give an example for “Flex Tree” which is aligned right side.
hi mani~
you’re welcome my email address is cenkhchuluun@yahoo.com
Can u please give an example of Flex tree control with disclosure icons pointing from right to left. Actually I want a tree control for rtl languages like arabic.
hi,
I’m looking for a working ‘rtl’ demo. Here the text align right and the direction ‘rtl’ give the same result. So everyone know how to get working rtl direction ?