Setting word wrapping on the Spark TextArea control in Flex 4
The following example shows how you can set word wrapping on a Flex 4 Spark TextArea control by setting the lineBreak style to one of the static constants in the LineBreak class (flashx.textLayout.formats.LineBreak):
- toFit (
LineBreak.TO_FIT): For lines that wrap to fit to the container width. - explicit (
LineBreak.EXPLICIT): For lines that break only at explicit return/line feeds.
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 trial, see http://www.adobe.com/products/flex/. To download the latest nightly build of the Flex 4 SDK, see http://opensource.adobe.com/wiki/display/flexsdk/Download+Flex+4.
For more information on getting started with Flex 4 and Flash Builder 4, see the official Adobe Flex Team blog.
<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2009/02/04/setting-word-wrapping-on-the-fxtextarea-control-in-flex-gumbo/ -->
<s:Application name="Spark_TextArea_lineBreak_test"
xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/halo">
<mx:ApplicationControlBar cornerRadius="0" width="100%">
<mx:Form styleName="plain">
<mx:FormItem label="lineBreak:">
<s:DropDownList id="comboBox" requiresSelection="true">
<s:dataProvider>
<s:ArrayList source="[toFit,explicit]" />
</s:dataProvider>
</s:DropDownList>
</mx:FormItem>
</mx:Form>
</mx:ApplicationControlBar>
<s:TextArea id="textArea"
lineBreak="{comboBox.selectedItem}"
fontSize="16"
horizontalCenter="0"
verticalCenter="0">
<s:content>
<s:p>1. The quick brown fox jumps over the lazy dog.</s:p>
<s:p>2. The quick brown fox jumps over the lazy dog.</s:p>
<s:p>3. The quick brown fox jumps over the lazy dog.</s:p>
<s:p>4. The quick brown fox jumps over the lazy dog.</s:p>
<s:p>5. The quick brown fox jumps over the lazy dog.</s:p>
<s:p>6. The quick brown fox jumps over the lazy dog.</s:p>
<s:p>7. The quick brown fox jumps over the lazy dog.</s:p>
<s:p>8. The quick brown fox jumps over the lazy dog.</s:p>
<s:p>9. The quick brown fox jumps over the lazy dog.</s:p>
</s:content>
</s:TextArea>
</s:Application>
You can also set the lineBreak style in an external .CSS file or <Style> block, as seen in the following example:
<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2009/02/04/setting-word-wrapping-on-the-fxtextarea-control-in-flex-gumbo/ -->
<s:Application name="Spark_TextArea_lineBreak_test"
xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/halo">
<fx:Style>
@namespace s "library://ns.adobe.com/flex/spark";
s|TextArea {
lineBreak: explicit;
}
</fx:Style>
<s:TextArea id="textArea"
fontSize="16"
horizontalCenter="0"
verticalCenter="0">
<s:content>
<s:p>1. The quick brown fox jumps over the lazy dog.</s:p>
<s:p>2. The quick brown fox jumps over the lazy dog.</s:p>
<s:p>3. The quick brown fox jumps over the lazy dog.</s:p>
<s:p>4. The quick brown fox jumps over the lazy dog.</s:p>
<s:p>5. The quick brown fox jumps over the lazy dog.</s:p>
<s:p>6. The quick brown fox jumps over the lazy dog.</s:p>
<s:p>7. The quick brown fox jumps over the lazy dog.</s:p>
<s:p>8. The quick brown fox jumps over the lazy dog.</s:p>
<s:p>9. The quick brown fox jumps over the lazy dog.</s:p>
</s:content>
</s:TextArea>
</s:Application>
Or, you can set the lineBreak style using ActionScript, as seen in the following example:
<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2009/02/04/setting-word-wrapping-on-the-fxtextarea-control-in-flex-gumbo/ -->
<s:Application name="Spark_TextArea_lineBreak_test"
xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/halo">
<fx:Script>
<![CDATA[
import mx.events.IndexChangedEvent;
private function comboBox_selectionChanged(evt:IndexChangedEvent):void {
textArea.setStyle("lineBreak", comboBox.selectedItem);
}
]]>
</fx:Script>
<mx:ApplicationControlBar cornerRadius="0" width="100%">
<mx:Form styleName="plain">
<mx:FormItem label="lineBreak:">
<s:DropDownList id="comboBox"
requiresSelection="true"
selectionChanged="comboBox_selectionChanged(event);">
<s:dataProvider>
<s:ArrayList source="[toFit,explicit]" />
</s:dataProvider>
</s:DropDownList>
</mx:FormItem>
</mx:Form>
</mx:ApplicationControlBar>
<s:TextArea id="textArea"
fontSize="16"
horizontalCenter="0"
verticalCenter="0">
<s:content>
<s:p>1. The quick brown fox jumps over the lazy dog.</s:p>
<s:p>2. The quick brown fox jumps over the lazy dog.</s:p>
<s:p>3. The quick brown fox jumps over the lazy dog.</s:p>
<s:p>4. The quick brown fox jumps over the lazy dog.</s:p>
<s:p>5. The quick brown fox jumps over the lazy dog.</s:p>
<s:p>6. The quick brown fox jumps over the lazy dog.</s:p>
<s:p>7. The quick brown fox jumps over the lazy dog.</s:p>
<s:p>8. The quick brown fox jumps over the lazy dog.</s:p>
<s:p>9. The quick brown fox jumps over the lazy dog.</s:p>
</s:content>
</s:TextArea>
</s: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/2009/02/04/setting-word-wrapping-on-the-fxtextarea-control-in-flex-gumbo/ -->
<s:Application name="Spark_TextArea_lineBreak_test"
xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/halo"
initialize="init();">
<fx:Script>
<![CDATA[
import flashx.textLayout.formats.LineBreak;
import mx.collections.ArrayList;
import mx.containers.ApplicationControlBar;
import mx.containers.Form;
import mx.containers.FormItem;
import mx.events.IndexChangedEvent;
import spark.components.DropDownList;
import spark.components.TextArea;
private var comboBox:DropDownList;
private var textArea:TextArea;
private function init():void {
var arr:Array = [LineBreak.TO_FIT, LineBreak.EXPLICIT];
comboBox = new DropDownList();
comboBox.dataProvider = new ArrayList(arr);
comboBox.requiresSelection = true;
comboBox.addEventListener(IndexChangedEvent.SELECTION_CHANGED, comboBox_change);
var formItem:FormItem = new FormItem();
formItem.label = "lineBreak:";
formItem.addChild(comboBox);
var form:Form = new Form();
form.styleName = "plain";
form.addChild(formItem);
var appControlBar:ApplicationControlBar = new ApplicationControlBar();
appControlBar.dock = true;
appControlBar.percentWidth = 100;
appControlBar.setStyle("cornerRadius", 0);
appControlBar.addChild(form);
addElementAt(appControlBar, 0);
var c:XML = <TextFlow xmlns="http://ns.adobe.com/textLayout/2008">
<p>1. The quick brown fox jumps over the lazy dog.</p>
<p>2. The quick brown fox jumps over the lazy dog.</p>
<p>3. The quick brown fox jumps over the lazy dog.</p>
<p>4. The quick brown fox jumps over the lazy dog.</p>
<p>5. The quick brown fox jumps over the lazy dog.</p>
<p>6. The quick brown fox jumps over the lazy dog.</p>
<p>7. The quick brown fox jumps over the lazy dog.</p>
<p>8. The quick brown fox jumps over the lazy dog.</p>
<p>9. The quick brown fox jumps over the lazy dog.</p>
</TextFlow>;
textArea = new TextArea();
textArea.content = c;
textArea.setStyle("fontSize", 16);
textArea.horizontalCenter = 0;
textArea.verticalCenter = 0;
addElement(textArea);
}
private function comboBox_change(evt:IndexChangedEvent):void {
textArea.setStyle("lineBreak", comboBox.selectedItem);
}
]]>
</fx:Script>
</s:Application>
Also, if you were using plain text instead of HTML formatted text (using the <p> and/or <br> tags) you would set the text property and use the “\n” escape sequence to create new lines, as seen in the following example:
<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2009/02/04/setting-word-wrapping-on-the-fxtextarea-control-in-flex-gumbo/ -->
<s:Application name="Spark_TextArea_lineBreak_test"
xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/halo"
initialize="init();">
<fx:Script>
<![CDATA[
import flashx.textLayout.formats.LineBreak;
import mx.collections.ArrayList;
import mx.containers.ApplicationControlBar;
import mx.containers.Form;
import mx.containers.FormItem;
import mx.events.IndexChangedEvent;
import spark.components.DropDownList;
import spark.components.TextArea;
private var comboBox:DropDownList;
private var textArea:TextArea;
private function init():void {
var arr:Array = [LineBreak.TO_FIT, LineBreak.EXPLICIT];
comboBox = new DropDownList();
comboBox.dataProvider = new ArrayList(arr);
comboBox.requiresSelection = true;
comboBox.addEventListener(IndexChangedEvent.SELECTION_CHANGED, comboBox_change);
var formItem:FormItem = new FormItem();
formItem.label = "lineBreak:";
formItem.addChild(comboBox);
var form:Form = new Form();
form.styleName = "plain";
form.addChild(formItem);
var appControlBar:ApplicationControlBar = new ApplicationControlBar();
appControlBar.dock = true;
appControlBar.percentWidth = 100;
appControlBar.setStyle("cornerRadius", 0);
appControlBar.addChild(form);
addElementAt(appControlBar, 0);
var c:String = "1. The quick brown fox jumps over the lazy dog.\\n" +
"2. The quick brown fox jumps over the lazy dog.\\n" +
"3. The quick brown fox jumps over the lazy dog.\\n" +
"4. The quick brown fox jumps over the lazy dog.\\n" +
"5. The quick brown fox jumps over the lazy dog.\\n" +
"6. The quick brown fox jumps over the lazy dog.\\n" +
"7. The quick brown fox jumps over the lazy dog.\\n" +
"8. The quick brown fox jumps over the lazy dog.\\n" +
"9. The quick brown fox jumps over the lazy dog.";
textArea = new TextArea();
textArea.text = c;
textArea.setStyle("fontSize", 16);
textArea.horizontalCenter = 0;
textArea.verticalCenter = 0;
addElement(textArea);
}
private function comboBox_change(evt:IndexChangedEvent):void {
textArea.setStyle("lineBreak", comboBox.selectedItem);
}
]]>
</fx:Script>
</s: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.
Peter deHaan
Peter deHaan currently works for Adobe on the Flex SDK QA team. While not working on Flex, Flash, and ColdFusion applications, Peter enjoys making up bios and writing in 3rd person. Peter's rarely updated blog can be found at blogs.adobe.com/pdehaan/, actionscriptexamples.com, airexamples.com, and coldfusionexamples.com.
-
Add Widgets (Content Sidebar)
This is your Content Sidebar. Edit this content that appears here in the widgets panel by adding or removing widgets in the Content Sidebar area.
8 Responses to Setting word wrapping on the Spark TextArea control in Flex 4
Leave a Reply Cancel reply
-
Categories
- Accordion
- AccordionHeader
- ActionScript
- AddChild
- AdvancedDataGrid
- Alert
- alpha
- Animate
- AnimateProperties
- Application
- Application (Spark)
- ArrayCollection
- BarChart
- baseColor
- beta
- beta1
- beta2
- Bitmap
- Bitmap/BitmapData
- BitmapData
- BitmapFill
- BitmapFill (Spark)
- BitmapGraphic
- BitmapImage
- BitmapImage (Spark)
- BitmapImageResizeMode
- Border (Spark)
- BorderContainer (Spark)
- Box
- BuildInfo
- Button
- Button (Spark)
- ButtonBar
- ButtonBar (Spark)
- ByteArray
- Camera
- Charting
- CheckBox
- CheckBox (Spark)
- ClassFactory
- CollectionEvent
- Color
- ColorPicker
- ColorUtil
- ComboBox
- ComboBoxArrowSkin
- Compiler
- Component
- Component (Spark)
- Configuration
- Container
- ContextMenu
- ContextMenuEvent
- ContextMenuItem
- CSSCondition
- CSSSelector
- CSSStyleDeclaration
- CurrencyFormatter
- CursorManager
- Data Binding
- DataGrid
- DataGrid (Spark)
- DataGridColumn
- Date
- DateBase
- DateChooser
- DateField
- DateFormatter
- Debugging
- DefaultComplexItemRenderer
- DefaultTileListEffect
- DropDownList
- DropDownList (Spark)
- DropDownListButtonSkin
- DropDownListSkin
- DropShadowFilter
- E4X
- Effects
- Ellipse
- EmailValidator
- Embed
- Event
- Fade
- FileFilter
- FileReference
- fill
- Filters
- Flash
- Flash Integration
- FlashVars
- Flex 3 SDK
- Flex Builder
- Flex Builder 3
- Flex SDK
- Flex4
- FLVPlayback
- FocusManager
- FontLookup
- Fonts
- Form
- Form (Spark)
- FormHeading (Spark)
- FormItem
- FormItem (Spark)
- Forms
- FTETextField (Spark)
- FullScreen
- FullScreenEvent
- FxAnimateColor
- FxButtonBar
- FxCheckBox
- FXG
- FxHScrollBar
- FxHSlider
- FxList
- FxNumericStepper
- FxRadioButton
- FxRotate3D
- FxScroller
- FxTextArea
- FxTextInput
- FxToggleButton
- FxVScrollBar
- FxVSlider
- getStyleDeclaration()
- GradientEntry
- Graphic (Spark)
- HBox
- HDividedBox
- HGroup (Spark)
- HorizontalLayout
- HorizontalList
- HSBColor (Spark)
- HScrollBar (Spark)
- HSlider
- HSlider (Spark)
- HTML template
- ID3Info
- Image
- Image (Spark)
- ImageSnapshot
- itemRenderer
- JointStyle
- Label
- Label (Spark)
- Legend
- LegendItem
- LigatureLevel
- Line
- LinearGradientStroke
- LineScaleMode
- LinkBar
- LinkButton
- List
- List (Spark)
- Menu
- MenuBar
- Metadata
- MetadataEvent
- Model
- Mouse
- MouseCursor
- MouseEvent
- Move
- Namespace
- NavigatorContent (Spark)
- needsSWF
- NetConnection
- NetStream
- Nightly Builds
- NumberBaseRoundType
- NumberFormatter
- NumberValidator
- NumericCompare
- NumericStepper
- NumericStepper (Spark)
- ObjectProxy
- ObjectUtil
- paddingLeft
- paddingRight
- Panel
- Panel (Spark)
- Parallel
- Path
- PieChart
- PieSeries
- PieSeriesItem
- PopUpAnchor (Spark)
- PopUpButton
- PopUpManager
- ProgrammaticSkin
- ProgressBar
- PropertyChangeEvent
- QName
- RadialGradient
- RadioButton
- RadioButton (Spark)
- RadioButtonGroup
- RadioButtonGroup (Spark)
- Rect
- RegExp
- Regular Expressions
- Repeater
- RichEditableText
- RichText
- RichText (Spark)
- RichTextEditor
- Rotate
- Rotate3D (Spark)
- Scroller (Spark)
- Sequence
- setStyle()
- SimpleText
- SimpleText (Spark)
- skinClass
- Slider
- SliderEvent
- SolidColor
- SolidColorStroke
- Sort
- SortField
- Sound
- SoundEffect
- Spinner (Spark)
- SpriteVisualElement (Spark)
- StageDisplayState
- States
- StringUtil
- StringValidator
- StyleManager
- Styles
- SWFLoader
- SWFObject
- System
- SystemManager
- TabBar
- TabBar (Spark)
- TabNavigator
- TabStopFormat
- Text
- Text Layout Framework (TLF)
- TextArea
- TextArea (Spark)
- TextBox
- TextConverter
- TextEvent
- TextFlow
- TextFlowUtil
- TextFormat
- TextGraphic
- TextInput
- TextInput (Spark)
- TextLayoutFormat
- TextView
- Themes
- TileLayout
- TileList
- TileOrientation
- Timer
- TitleWindow
- TitleWindow (Spark)
- TLF
- ToggleButton (Spark)
- ToggleButtonBar
- ToolTip
- Transition
- Tree
- TruncationOptions
- UIComponent
- UIFTETextField
- Updater
- URLLoader
- URLRequest
- URLUtil
- URLVariables
- ValidationResultEvent
- Validator
- Validators
- VBox
- VDividedBox
- Vector
- VerticalLayout
- VerticalLayout (Spark)
- VGroup (Spark)
- Video
- VideoDisplay
- VideoElement
- VideoElement (Spark)
- VideoEvent
- VideoPlayer (Spark)
- VideoPlayerScrubBar
- ViewStack
- VScrollBar (Spark)
- VSlider
- VSlider (Spark)
- XML
- XMLList
- XMLListCollection
- ZipCodeValidator
- ZipCodeValidatorDomainType
- Zoom
-
Articles
- December 2010
- November 2010
- October 2010
- September 2010
- August 2010
- July 2010
- June 2010
- May 2010
- April 2010
- March 2010
- February 2010
- January 2010
- December 2009
- November 2009
- October 2009
- September 2009
- August 2009
- July 2009
- June 2009
- May 2009
- April 2009
- March 2009
- February 2009
- January 2009
- December 2008
- November 2008
- October 2008
- September 2008
- August 2008
- July 2008
- June 2008
- May 2008
- April 2008
- March 2008
- February 2008
- January 2008
- December 2007
- November 2007
- October 2007
- September 2007
- August 2007
- July 2007
-
Meta


Hi,
I’ve tried this example in flexbuilder build 3.0.205647 with flex gumbo build 4.0.0.4904
and it didn’t work.
I linked the flashplayer 10 swc in the project’s flex build path.
It compiled fine but when i opened it in the browser it doesn’t display anything.
I right clicked in the window and the flash player is there.
What am i missing?
Example(s) updated to work with Flex SDK 4.0.0.6731.
Hello Peter.
Could you clarify for me one question: the escape character – ‘\n’ – does break the line of text in ActionScript block, but I’d like to use the multiline property of Spark Label to break the lines of text as I need it within MXML. Something like this:
<s:Label text="First line \n Second line" />Whether I put it with preceding ‘\’ or plainly or wrap it in single quotes I just see it faithfully reproduced as a text. Managing this with ‘width’ of a Label, or constraints like ‘left’ or ‘right’ is rather inconvenient.
Thanks,
Igor
@Igor,
You need to wrap the “\n” with data binding:
I’ll see if I can find the SDK/compiler bug for this. I think there was another workaround.
Peter
Well, I found at least 4 versions of this reported in JIRA. I believe the last one (SDK-12649) is the bug I was remembering:
http://bugs.adobe.com/jira/browse/SDK-266 – “Can’t insert newline in text without using htmlText”
http://bugs.adobe.com/jira/browse/SDK-1352 – “need a way to insert a newline character in MXML String values”
http://bugs.adobe.com/jira/browse/SDK-8963 – “textarea component \n \r does not work”
http://bugs.adobe.com/jira/browse/SDK-11469 – “Line breaks don’t work from attribute text, but works for AS and Binding. Should be consistent.”
http://bugs.adobe.com/jira/browse/SDK-12649 – “Erratic behavior in line break display”
Feel free to vote for all those bugs in JIRA so Adobe knows this issue is important to you and we can prioritize our bugs accordingly.
Thanks,
Peter
This also seems to work:
Peter
Dear Peter,
Thank you so much. Both syntaxes work, and that’s what matters most (at least at the moment). I’ll add my vote for fixing the problem.
Just mater of curiosity: where could I find the explanations of why binding (this is not actual binding, is it?) to an escape character works?
Igor
I believe this is data binding, you’re just binding to a static string (“\n”). I think it gets treated by the compiler differently, or is causing it to get set by ActionScript in the internals of the Flex architecture (but what do I know, that may all be wrong).
I read through each of those 5 bugs, and the best explanation is given in SDK-11469 and there is an additional workaround given in SDK-12649. And actually, another simple workaround for this is to set the
textproperty via ActionScript instead of directly in MXML. It may also be marginally better performance wise, then using some binding to the static “\n” string. But yes, I do believe this is a compiler issue where it is seeing a “\n” and escaping the backslash so it gets preserved. Based on the 5 bugs, I was never 100% certain whether it was a bug or fact of life or what, but I definitely think it is a usability issue since it seems to be coming up again and again by internal and external people. I’m hoping that we can get http://bugs.adobe.com/jira/browse/SDK-12649 investigated by a current member of the compiler team to get a definitive answer on what the problem and solution (if any) is, but for now, it seems like the various workarounds work.If you want to see what is happening within Flex, it should be relatively easy to add the -keep compiler option to a simple “hello\nworld” test case and see what the generated code is.
Peter