简体   繁体   中英

how to make text line break in flex textarea

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?

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

不是{\\ n}而是{'\\ n'}

@radekg

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

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

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." 

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?

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.

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

You should do:

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

That's it.

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"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM