flash中嵌入字体实现原理及代码

平面设计 2021-07-03 14:34www.168986.cn平面设计培训
1、建立FontLibrary.fla文件,在其文档类FontLibrary.as中写如下代码

复制代码
代码如下:

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..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,pleteHandler);
}
private function pleteHandler(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);
}
}
}

Copyright © 2016-2025 www.168986.cn 狼蚁网络 版权所有 Power by