PDA

View Full Version : [AS3] Help with formating text of ui components


nikolaibelkin
11-30-2008, 07:09 AM
Hi, I'm new to actionscript, so this may seem like a stupid question but here goes:

I am trying to change the text format of 2 gui elements(e.g. 2 lists) by attaching 2 different TextFormat instances to each element. The problem is that for some reason myList.setStyle("textFormat", format1); doesn't seem to work as the text doesn't visually change. However, StyleManager.setStyle("textFormat", format2); works but it applies one format to both of the elements, but i want each element to have a different format.

Here's the code:
import fl.controls.List;
import fl.managers.StyleManager;

var format1:TextFormat = new TextFormat();
format1.font = "Verdana, Arial, Helvetica";
format1.bold = true;

var format2:TextFormat = new TextFormat();
format2.font = "Courier new";
format2.color = 0xFF0000;

var myList:List = new List();
myList.move(10,10);
myList.addItem({label:"Testing"});
myList.addItem({label:"with some"});
myList.addItem({label: "values"});
addChild(myList);

myList.setStyle("textFormat", format1); // doesn't work
trace(myList.getStyle("textFormat").font);//Verdana, Arial, Helvetica
StyleManager.setStyle("textFormat", format2); // works but it is global, not what i want.
trace(myList.getStyle("textFormat").font);//Verdana, Arial, Helvetica
StyleManager.setStyle("textFormat", format1); // also works and overwrites above line.
trace(myList.getStyle("textFormat").font);//Verdana, Arial, Helvetica
myList.setStyle("textFormat", format1); // doesn't overwrite the above line.(but i think it should)
trace(myList.getStyle("textFormat").font);//Verdana, Arial, Helvetica
myList.clearStyle("textFormat"); // doesn't work
trace(myList.getStyle("textFormat").font);//Error #1009: Cannot access a property or method of a null object reference.

var anotherList:List = new List();
anotherList.move(150,10);
anotherList.addItem({label:"Testing"});
anotherList.addItem({label:"with some"});
anotherList.addItem({label: "values"});
addChild(anotherList);
myList.setStyle("textFormat", format2); // doesn't work

Thanks in advance for your help!:)