在ActionScript 3中更改字体系列和字体大小

时间:2016-11-20 23:55:19

标签: xml actionscript-3 flash fonts

我正在尝试使用XML和Action Script 3将文本嵌入到Flash中。我发现代码对我有用,但我不知道如何在Action Script中更改字体和字体大小。

你能帮帮我吗?谢谢!

我的AS3 fie看起来像这样:

var yPlacement:int = 20;
var xPlacement:int = 30;
var distance:int = 60;
var myFormat:TextFormat = new TextFormat(); 
myFormat.color = 0xAA0000; 
myFormat.size = 24; 
myFormat.italic = true; 
myFormat.align = TextFormatAlign.CENTER 
var myXML:XML = new XML();
var XML_URL:String = "myXMLFile.xml";
var myXMLURL:URLRequest = new URLRequest(XML_URL);
var myLoader:URLLoader = new URLLoader(myXMLURL);
myLoader.addEventListener("complete", xmlLoaded);
function xmlLoaded(event:Event):void {
myXML = XML(myLoader.data);
var xmlDoc:XMLDocument = new XMLDocument();
xmlDoc.ignoreWhite = true;
var menuXML:XML = XML(myLoader.data);
xmlDoc.parseXML(menuXML.toXMLString());
for each (var ListItem:XML in myXML..ListItem) {
var listColor:String = ListItem.itemColor.toString();
var listLabel:String = ListItem.itemLabel.toString();
var listPhone:String = ListItem.itemPhone.toString(); 
var myText1:TextField = new TextField();
myText1.text = listLabel;
myText1.autoSize = TextFieldAutoSize.LEFT;
myText1.x = 2;
myText1.y = 2; 
var myText2:TextField = new TextField();
myText2.text = listPhone;
myText2.autoSize = TextFieldAutoSize.LEFT;
myText2.x = 2;
myText2.y = 20; 
var clip_mc = new MovieClip();
clip_mc.addChild(myText1);
clip_mc.addChild(myText2);
addChild(clip_mc); 
clip_mc.y = yPlacement; 
clip_mc.x = xPlacement; 
yPlacement = yPlacement + distance; 
}
}

XML文件看起来像:

<?xml version="1.0" encoding="utf-8"?>
<XML>
<myXMLList>
<ListItem>
<itemColor>FFFFFF</itemColor>
<itemLabel>NÃO ACORDES O DRAGÃO</itemLabel>
<itemPhone>SÁBADO - 14H00</itemPhone>
<itemSize>32</itemSize>
</ListItem> 
<ListItem>
<itemColor>FFE8E8</itemColor>
<itemLabel>Emily Owen</itemLabel>
<itemPhone>888-888-8888</itemPhone> 
</ListItem> 
<ListItem>
<itemColor>DDFDB3</itemColor>
<itemLabel>Jeff Jones</itemLabel>
<itemPhone>666-666-6666</itemPhone> 
</ListItem> 
<ListItem>
<itemColor>DDFDB3</itemColor>
<itemLabel>Hello</itemLabel>
<itemPhone>666-666-6666</itemPhone> 
</ListItem>
</myXMLList>
</XML>

2 个答案:

答案 0 :(得分:1)

我在这里找到了这个,但没有测试过: http://help.adobe.com/en_US/ActionScript/3.0_ProgrammingAS3/WS5b3ccc516d4fbf351e63e3d118a9b90204-7d87.html

理论上,它看起来像这样(结合你的代码和例子):

var myText1:TextField = new TextField();
myText1.text = listLabel;
myText1.autoSize = TextFieldAutoSize.LEFT;
myText1.x = 2;
myText1.y = 2; 

var format1:TextFormat = new TextFormat(); 
format1.size= 14; 

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

myText1.setTextFormat(format1); 
myText1.setTextFormat(format2);

答案 1 :(得分:1)

每次更改文本时,必须在更改文本后调用setTextFormat()方法。 如果要嵌入字体,请确保您的库中有字体:

enter image description here

enter image description here

如果文本发生变化而您没有调用setTextFormat(params ...) TextFormat将被忽略。

祝你好运。 尼古拉斯