AS3 - TextField:嵌入字体

时间:2013-07-18 11:09:50

标签: actionscript-3 flash flash-builder flash-builder4.5

此代码不会将文本呈现到屏幕上。改变,

drawText.embedFonts = false;

呈现文字,但字体大小不会修改或颜色。

package {

import flash.display.Sprite;
import flash.text.TextField;
import flash.text.TextFormat;
import flash.text.*;

public class DrawText extends Sprite {

    private var drawText:TextField;
    private var myFormat:TextFormat;

    [Embed(source="c:/windows/fonts/verdana.ttf", fontFamily="Verdana", embedAsCFF="false")]
    private var verdana:Class;
    public function DrawText(mX:int,mY:int){

        myFormat = new TextFormat("Verdana");
        myFormat.size = 32;
        myFormat.color = 0x00FFFF;

        drawText = new TextField();
        drawText.embedFonts = true;
        drawText.autoSize = TextFieldAutoSize.LEFT;
        drawText.selectable = false;
        drawText.type = "dynamic";
        drawText.multiline=true;
        drawText.wordWrap=true;
        drawText.x = 128;
        drawText.y = 128;
        drawText.text = "TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST";
        drawText.defaultTextFormat = myFormat;
        addChild(drawText);

    }//END constructor

}//END class

}//END package

非常令人沮丧的是,任何帮助都会非常感激。我正在使用Flash Builder 4.6。

2 个答案:

答案 0 :(得分:5)

您应该在设置defaultTextFormat之前应用text,或者对现有文本使用TextField.setTextFormat

UPD: 对于embedFonts,您必须在使用前注册字体类:

Font.registerFont(verdana);

UPD2:

示例(修改主题中的代码):

   //set defaultTextFormat before set text 
   //and use setTextFormat to format existed text 
   drawText.defaultTextFormat = myFormat;
   drawText.setTextFormat(myFormat);
   drawText.text = "TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST";

答案 1 :(得分:1)

您应该使用drawText.setTextFormat(myFormat);