flash中嵌入字体实现原理及代码
程序员文章站
2022-05-12 20:17:52
在flash中嵌入字体,相信有很多的童鞋们对此还是很陌生的,不过没关系,因为本文的出现将会解除你的疑惑,感兴趣的朋友可以了解下,或许对你有所帮助... 13-02-05...
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.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);
}
}
}
复制代码
代码如下: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);
}
}
}
上一篇: JS实现页面打印功能