25

I have a string

var s:String = "This is a line \n This is another line.";
this.txtHolder.text = s; //.text has \n, not a new line

and i want to put it into a text area, but the new line character is ignored. How can i ensure that the text breaks where i want it to when its assigned?

Cameron A. Ellis
  • 3,833
  • 8
  • 38
  • 46

9 Answers9

44

On flex, while coding \n is working well on mxml or any xml to define a line just use 
 line entity.

I mean:

lazy
fox

gives us

lazy<br />
fox
Francisco Romero
  • 12,787
  • 22
  • 92
  • 167
9

not {\n} but {'\n'}

5

@radekg

The OP is referring to the text string written in MXML syntax:

<mx:TextArea text="This is a &#13; new line" />
noobular
  • 3,257
  • 2
  • 24
  • 19
3

Try

"This is a line {\n} This is another line."

Alternatively, use the htmlText attribute and use

"This is a line <br> This is another line." 
Hates_
  • 66,613
  • 6
  • 32
  • 37
0

I just tested following code:

<?xml version="1.0" encoding="utf-8"?>
<mx:WindowedApplication xmlns:mx="http://www.adobe.com/2006/mxml"
    creationComplete="onComplete();">
    <mx:Script>
        <![CDATA[
            private function onComplete():void {
                var s:String = "This is a line \n This is another line.";
                this.txtHolder.text = s;
            }
        ]]>
    </mx:Script>
    <mx:TextArea id="txtHolder" />
</mx:WindowedApplication>

and with mx:Text

<?xml version="1.0" encoding="utf-8"?>
<mx:WindowedApplication xmlns:mx="http://www.adobe.com/2006/mxml"
    creationComplete="onComplete();">
    <mx:Script>
        <![CDATA[
            private function onComplete():void {
                var s:String = "This is a line \n This is another line.";
                this.txtHolder.text = s;
            }
        ]]>
    </mx:Script>
    <mx:Text id="txtHolder" />
</mx:WindowedApplication>

Both are working just fine. Maybe you're using mx:TextInput or mx:Label?

0

In Flex if you are trying to place line next to previous line. Then just append it to previous line.

var line:String="Hello";
textarea1.text += line;

Now textarea1 which is your textarea in which you want to print this string will append to it.

Anuj Sharma
  • 4,294
  • 2
  • 37
  • 53
0

I just did this as follow,

protected function addToTextArea(array:Array):void
            {
                textArea.text = "Array Elements are:";
                for(var k:int = 0; k < array.length; k=k+1)
                 {
                    textArea.text = textArea.text +"\n"+ array[k];
                 }
            }

Thank you Tolgahan ALBAYRAK

Kans
  • 13
  • 2
0

You should do:

var s:String = "This is a line" + "\n" + "This is another line.";
this.txtHolder.text = s;

That's it.

Matteo Baldi
  • 5,613
  • 10
  • 39
  • 51
Jameel
  • 1
0

It should work or at the very least < br \> (without the spaces before the "br") should work if you are using htmlText.

I was using XML to fill in the TextArea and since I'm not entirely sure how to use HTML inside of XML (they mention that I should wrap it with CDATA tags) but I just did a simple

txt.replace("\\n", "<br/>");

Perhaps there's a better way to go about it but this works out nicely.

EDIT: I had a space after the "br"

nevets1219
  • 7,692
  • 4
  • 32
  • 47