您的位置:首页 > 其它

在纯AS工程中嵌入个别字体方法 (转载)

2010-09-25 18:36 330 查看

在纯AS工程中嵌入个别字体方法

2009-11-26 10:00
前两天接到主管任务,让研究在纯AS工程中嵌入个别字体。在网上找了一大通,也只找到了在Flex项目中使用CSS也嵌入个别字体的方法。最后很无奈的发现网上好像没有我想找的资料。就在此时让我有个想法,在CSS中控制嵌入字体使用的是unicodeRange属性,那我能不能在Embed标签中也使用它呢?实验发现,Embed标签中是可以使用unicodeRange属性的。下面说说我在纯AS工程中是怎么嵌入个别字体的。

工程开始之前先告诉大家,我要嵌入的字符是"空山无人,水流花开。",要使用的字体是楷体字;

1.打包要嵌入字体到一个Swf文件中

(1).新建一个纯AS工程,内容如下:

代码

整个工程如下:

package
{
import flash.display.Loader;
import flash.display.Sprite;
import flash.display.StageAlign;
import flash.display.StageQuality;
import flash.display.StageScaleMode;
import flash.events.Event;
import flash.events.ProgressEvent;
import flash.net.URLRequest;
import flash.system.LoaderContext;
import flash.text.Font;
import flash.text.TextField;
import flash.text.TextFormat;
import flash.utils.getDefinitionByName;

public class FontTest extends Sprite
{
private var txt:TextField;
private var fontLoader:Loader;
public function FontTest():void
{
if(stage!=null){
init();
}else{
addEventListener(Event.ADDED_TO_STAGE,init);
}
}
private function init():void{
stage.quality = StageQuality.HIGH;
stage.scaleMode = StageScaleMode.NO_SCALE;
stage.align = StageAlign.TOP_LEFT;
stage.showDefaultContextMenu = false;

txt = new TextField();
txt.x = stage.stageWidth/3;
txt.y = stage.stageHeight/2;
txt.width = 500;
txt.height = 30;
addChild(txt);

fontLoader = new Loader();
fontLoader.load(new URLRequest("LoadFont.swf"), new LoaderContext(false, loaderInfo.applicationDomain));
fontLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, completeHandler);
fontLoader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, progressHandler);
}
private function progressHandler(e:ProgressEvent):void
{
var ratio:int = e.bytesLoaded / e.bytesTotal * 100;
txt.text = "loading... " + ratio + "%";
}

private function completeHandler(e:Event):void
{
fontLoader.contentLoaderInfo.removeEventListener(Event.COMPLETE, completeHandler);
fontLoader.contentLoaderInfo.removeEventListener(ProgressEvent.PROGRESS, progressHandler);
var MyFont:Class = getDefinitionByName("LoadFont_RoundHand") as Class;
// 注册全局字体
Font.registerFont(MyFont);
var myFont:Font = new MyFont() as Font;
// 应用字体
var format:TextFormat = new TextFormat(myFont.fontName, 25, null, true);
txt.defaultTextFormat = format;
// 嵌入字体
txt.embedFonts = true;
txt.text = "空山无人,水流花开。";
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐