flash 嵌入字体方法

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); 
        } 
    } 
}

可以去这个网页自动生成类代码 http://kevincao.com/app/font/

posted @ 2013-02-05 10:15  【Winco】  阅读(544)  评论(0编辑  收藏  举报