复制代码
代码如下:package {
import flash.text.Font;
public class FontLibrary extends Sprite {
[Embed(systemFont="华文中宋", fontName="MyFont", unicodeRange="U+8f93,U+5165,U+6587,U+5b57", mimeType="application/x-font")]
static public var MyClass : Class;
public function FontLibrary() {
Font.registerFont(MyClass);
}
}
}
如果不是嵌入系统字体,而是嵌入外部路径的字体,则按照如下写:
复制代码
代码如下:package {
import flash.text.Font;
public class FontLibrary extends Sprite {
[Embed(source="pages/simhei.ttf", fontName="MyFont", unicodeRange="U+8f93,U+5165,U+6587,U+5b57", mimeType="application/x-font")]
static public var MyClass : Class;
public function FontLibrary() {
Font.registerFont(MyClass);
}
}
}
执行编译生成FontLibrary.swf
2、调用嵌入的字体文件,在主程序中写如下代码:
复制代码
代码如下:package{
import flash.display.MovieClip;
import flash.display.Loader;
import flash.events.Event;
import flash.net.URLRequest;
import flash.text.TextFormat;
import flash.text.TextField;
import flash.text.TextFieldAutoSize;
public class Main extends MovieClip{
private var fontLoader:Loader;
private var txtFmt:TextFormat;
private var txtFld:TextField;
public function Main(){
fontLoader = new Loader();
fontLoader.load(new URLRequest("FontLibrary.swf"));
fontLoader.contentLoaderInfo.addEventListener(Event.COMPLETE,completeHandler);
}
private function completeHandler(e:Event):void{
txtFmt=new TextFormat("MyFont",68,0x000000);
txtFld=new TextField();
txtFld.autoSize=TextFieldAutoSize.LEFT;
txtFld.embedFonts=true;
txtFld.text="显示中文";
txtFld.setTextFormat(txtFmt);
addChild(txtFld);
}
}
}