views:

11086

answers:

6

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?

A: 

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_
A: 

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
+5  A: 

on flex, while coding '\n' is working well.. on mxml or any xml to define a line just use '&#13;' line entity.. i mean :

lazy&#13;fox

gives us

lazy
fox

A: 

Hi, 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?

radekg
A: 

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

Guillaume Morin
A: 

@radekg

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

<mx:TextArea text="This is a &#13; new line" />
noobular