PDA

View Full Version : XML & Textfield formatting



SandeR2
May 12th, 2009, 10:23 AM
Hi,

Currently I'm working on a system to let customers write a small comment.
I'm using a xml file to load the comments, but somehow something goes wrong when I try to give it some special formatting.

Here is my as3 code:

package classes{

import flash.display.Sprite;
import flash.display.Graphics;
import flash.xml.*;
import flash.events.*;
import flash.net.*;
import flash.text.TextField;
import flash.text.TextFormat;

public class Comments extends Sprite {

private var xmlLoader:URLLoader;
private var xmlData:XML;
private var commentChildren:XMLList;
private var speak:Speaker;
private var txt:TextField;
private var tFmt:TextFormat;

public function Comments() {

tFmt = new TextFormat;
tFmt.color = 0xF0F0F0;
tFmt.font = "Complete in him";
tFmt.size = 14;

xmlLoader = new URLLoader();
xmlData = new XML();

xmlLoader.addEventListener(Event.COMPLETE, loadXML);

xmlLoader.load(new URLRequest("comments.xml"));

function loadXML(e:Event):void {

xmlData = new XML(e.target.data);
parseComments(xmlData);
}
function parseComments(commentsInput:XML):void {
commentChildren = commentsInput.comment.child("message");

for (var i:int = 0; i < commentChildren.length(); i++) {
graphics.beginFill(0x3d3d3d);
graphics.drawRoundRect(20,20+77*i,200,75,10,10);
graphics.endFill();

txt = new TextField();
txt.x = 25;
txt.y = 25+77*i;
txt.height = 65;
txt.width = 190;
txt.defaultTextFormat = tFmt;
txt.htmlText = commentChildren[i];
txt.wordWrap = true;
txt.embedFonts = true;
txt.antiAliasType = "ADVANCED";
txt.selectable = false;
txt.background = true;
txt.backgroundColor = 0xFF0000;
addChild(txt);
}
for (i = 0; i < commentChildren.length()-1; i++) {
speak = new Speaker();
speak.x = 180;
speak.y = 95+77*i;
addChild(speak);
}
}
}
}
}My XML code:

<comments>
<comment>
<message>Musters Koeriers # Lorem ipsum dolor sit amet, consectetur adipiscing elit. In eget dui purus. Lorem ipsum dolor sitar.</message>
</comment>
<comment>
<message><font color="#ccff00">Gipel Tilburg</font>Lorem ipsum dolor sit amet, consectetur adipiscing elit. In eget dui purus. Lorem ipsum dolor sitar.</message>
</comment>
<comment>
<message><font color="#ccff00">Bink Interieurs</font>Lorem ipsum dolor sit amet, consectetur adipiscing elit. In eget dui purus. Lorem ipsum dolor sitar.</message>
</comment>
</comments>
Now as you can see in the xml the 1st message is without any formatting, the 2nd and 3rd are. In the attached comment.jpg you can see what the result is. I can't figure out where the "enter" and those extra "spaces" are coming from but I suspect it's from the 'font' tags.

btw. those red background are just the textField backgrounds to show where the text should start.

Did anyone else come across this problem, and how can it be fixed.
Thanks in advance.

_Sander2

SandeR2
May 18th, 2009, 04:04 AM
I hate putting a topic back on top like this, but I still haven't figured out the problem :(

kunjan
May 19th, 2009, 03:30 AM
Hello,

if you put trace( txt.htmlText);
on line number 60 in code in function parseComments in for loop..

then you will got the idea why the first message is not formatting and 2nd & 3rd are...

you can get following in output window...

================================================

<P ALIGN="LEFT"><FONT FACE="Complete in him" SIZE="14" COLOR="#F0F0F0" LETTERSPACING="0" KERNING="0">Musters Koeriers # Lorem ipsum dolor sit amet, consectetur adipiscing elit. In eget dui purus. Lorem ipsum dolor sitar.</FONT></P>

<P ALIGN="LEFT"><FONT FACE="Complete in him" SIZE="14" COLOR="#F0F0F0" LETTERSPACING="0" KERNING="0"></FONT></P><P ALIGN="LEFT"><FONT FACE="Complete in him" SIZE="14" COLOR="#F0F0F0" LETTERSPACING="0" KERNING="0"> <FONT COLOR="#CCFF00">Gipel Tilburg</FONT></FONT></P><P ALIGN="LEFT"><FONT FACE="Complete in him" SIZE="14" COLOR="#F0F0F0" LETTERSPACING="0" KERNING="0"> Lorem ipsum dolor sit amet, consectetur adipiscing elit. In eget dui purus. Lorem ipsum dolor sitar.</FONT></P>

<P ALIGN="LEFT"><FONT FACE="Complete in him" SIZE="14" COLOR="#F0F0F0" LETTERSPACING="0" KERNING="0"></FONT></P><P ALIGN="LEFT"><FONT FACE="Complete in him" SIZE="14" COLOR="#F0F0F0" LETTERSPACING="0" KERNING="0"> <FONT COLOR="#CCFF00">Bink Interieurs</FONT></FONT></P><P ALIGN="LEFT"><FONT FACE="Complete in him" SIZE="14" COLOR="#F0F0F0" LETTERSPACING="0" KERNING="0"> Lorem ipsum dolor sit amet, consectetur adipiscing elit. In eget dui purus. Lorem ipsum dolor sitar.</FONT></P>

================================================

this problem due to when you give any formatting in xml then store in txt.htmlText it will put extra html tag with you string in this case it will put extra <p> </p> tag

Regards,
kunjan.

SandeR2
May 19th, 2009, 06:27 AM
Are you saying if I do the formatting in the xml itself without the <p> tags it will be fixed?
I'll give it a try and let you know.

Edit: It worked thanks allot :)