Skip to main content
BrianStapleton
Known Participant
June 22, 2009
Answered

Line Breaks in Dynamic Text

  • June 22, 2009
  • 1 reply
  • 2488 views

I've finally got XML information coming into my text field (thanks Ned!). Only problem now is that there for every line break in the text, Flash reads it as two line breaks, for example:

XML:

Line 1

Line 2

Flash:

Line 1


Line 2

I've searched forums and tutorials and found that you can just use the <br> tag in the original text or if I'm using actionscript to input the text I can use \n or \r but what if I'm using text generated by a CMS which my client needs to be able to use?

This topic has been closed for replies.
Correct answer kglad

No, actually. This is the script I'm using:

// load css
var format = new TextField.StyleSheet();
var path = "http://www.website.com/flash.css";
format.load(path);

vacancy_list_arr = new Array();
vacancy_list_xml = new XML();
vacancy_list_xml.ignoreWhite = true;

vacancy_list_xml.onLoad = function(success) {
    if (success) {
        var job_xml = vacancy_list_xml.firstChild.firstChild;
        while (job_xml != null) {
            // add the job data to our vacancy_list_arr
            vacancy_list_arr.push(getTrackData(job_xml));
            job_xml = job_xml.nextSibling;
        }
    } else {
        trace("Error loading vacancy_list.");
    }
    delete vacancy_list_xml;
    for(i=0; i<vacancy_list_arr.length; i++){
        var jobPosition = "<p class='red'>Job Position</p>"
        var jobLocation = "<p class='red'>Location</p>"
        var jobDescription = "<p class='red'>Description</p>"
        detailText.styleSheet = format;
        detailText.text = jobPosition + vacancy_list_arr.Headline + newline + newline + jobLocation + vacancy_list_arr.Location + newline + newline + jobDescription + vacancy_list_arr.Description;
        detailText.text = detailText.text.split("\r\n").join("\r").split("\n\r").join("\r");
    }
}

//

function getTrackData(job_xml) {
    // convert data to object form
    var jobData = new Object();
    var data_xml = new XML();
    data_xml = job_xml.firstChild;
    while (data_xml != null) {
        jobData[data_xml.nodeName] = data_xml.firstChild.nodeValue;
        data_xml = data_xml.nextSibling;
    }
    return jobData;
}

// load xml after css is loaded
format.onLoad = function(success){
    vacancy_list_xml.load("http://www.website.com/jobdetail.asp?NID="+_root.moreGlobalData);
}


enable the html property of your textfield and assign htmlText instead of text.

1 reply

kglad
Community Expert
Community Expert
June 22, 2009

enable the ignoreWhite property of your xml instance:

yourxml.ignoreWhite=true;

Inspiring
June 22, 2009

Although I would suggest that any XML loaded text that you plan to display in Flash should probably be wrapped in a CDATA tag and ignoreWhite won't affect that. If you plan to use things like <B> tags and all the CDATA prevents the XML parser from seeing that tag as another childnode.

I think part of the problem is that certain text editors put both a linefeed (\n) and return (\r) into their text streams.

We have a lot of problems with this and I don't really have any good solutions.

BrianStapleton
Known Participant
June 25, 2009

Thanks for the replies.

I was actually already using ignore.white but it wasn't working.

Just tried wrapping the XML in CDATA tags but that didn't work either.