Hey,buddy! How are you doing these days?All right,Let us straight to the point.
flex嵌入字体有两种方式:
方式1,直接嵌入:
package
{
import flash.display.Sprite;
public class Afff extends Sprite
{
[Embed(mimeType='application/x-font', source='font/minicartoon.ttf', fontName='迷你简卡通')]
public var mnCartonFont:Class;
public var fonts:Array=[];
public function Afff()
{
this.fonts.push(mnCartonFont);
}
}
}
或者:
<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx"
minWidth="955" minHeight="600" fontFamily="MS_YAHEI">
<fx:Script>
<![CDATA[
[Embed(source="assets/font/微软雅黑.ttf",fontName="MS_YAHEI",mimeType="application/x-font")]
private var ArialFont:Class;
]]>
</fx:Script>
<s:Label text="系统设置" buttonMode="true" useHandCursor="true" color="#FFFFFF"/>
</s:Application>
方式2,将字体打包成swf,然后运行加载:
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="horizontal" initialize="init()">
<mx:Script>
<![CDATA[
import mx.core.UIComponent;
private var tf:TextField;
private var fontLoader:Loader;
private var swfUrl:String = "font/MiniCartoonFont.swf";
private var allFonts:Array = new Array();
//运行加载
private function init():void {
fontLoader = new Loader();
fontLoader.contentLoaderInfo.addEventListener(Event.COMPLETE,loadCompleteHander);
fontLoader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR,loadFailHandler);
var re:URLRequest = new URLRequest(swfUrl);
fontLoader.load(re);
}
private function loadCompleteHander(event:Event):void {
var loaderInfo:LoaderInfo = event.target as LoaderInfo;
if (loaderInfo && loaderInfo.contentType == "application/x-shockwave-flash"){
var className:String = getFilename(loaderInfo.url);
var font:Class = loaderInfo.applicationDomain.getDefinition(className) as Class;
var fonts:Array=new font().fonts;
if (fonts){
var len:uint=fonts.length;
for (var i:uint=0; i < len; i++){
Font.registerFont(fonts[i]);
}
}
}
allFonts.push("宋体");
var tfs:Array = Font.enumerateFonts(false);
for (var k:int=0; k < tfs.length; k++){
allFonts.push(tfs[k].fontName);
}
fontCom.dataProvider = allFonts;
}
private function getFilename(path:String):String {
var pathStr:String=path;
var filename:String;
var index:int;
index=pathStr.lastIndexOf("/");
if (index != -1){
filename=pathStr.substr(index + 1);
}else{
filename=pathStr;
}
index=filename.lastIndexOf(".");
if (index != -1){
filename=filename.substr(0, index);
}
return filename;
}
private function loadFailHandler(event:Event):void {
}
private function addEmbedText():void {
tf = new TextField();
tf.wordWrap = true;
tf.width = tf.height = 300;
tf.text = "baby,I love you!"+"/n"+"在这片美丽的土地上,人们快乐的生活着。";
var ui:UIComponent = new UIComponent();
ui.percentWidth = ui.percentHeight = 100;
ui.addChild(tf);
myCan.addChild(ui);
}
private function chooseFontHandler():void {
if(fontCom.selectedItem == null){
return;
}
var str:String = fontCom.selectedItem.toString();
if(str == "迷你简卡通"){
tf.embedFonts = true;
}else {
tf.embedFonts = false;
}
var fm:TextFormat = new TextFormat();
fm.font = str;
fm.size = 25;
tf.setTextFormat(fm);
}
]]>
</mx:Script>
<mx:Canvas id="myCan" width="350" height="350" borderStyle="solid"/>
<mx:Button label="增加嵌入文字" click="addEmbedText()"/>
<mx:ComboBox id="fontCom" change="chooseFontHandler()"/>
</mx:Application>
运用过程中需要注意的几点:
1.使用嵌入字体一定要打开component的embedFonts属性,即设为true,当该属性值为true的时候,系统的字体讲不可用,若要用需重新关闭该属性。
2.flexSDK3.3之前若使用嵌入字体是有一bug,即当textField组件(或者其他的文字输入组件)没有文字内容时,文字不能输入,本身有内容才可获得光标进行输入,这个bug听说在sdk3.5时已经得到解决,但个人没有人确认过。目前我的解决办法是当文字为空时始终保持文字有一个空格,并且让光标停在该空格后,这时你就可以输入文字了。
3.当装载字体的swf与主程序的swf不是在同一个域中是请为Loader的load方法构造context参数即:
var context:LoaderContext = new LoaderContext();
context.applicationDomain = new ApplicationDomain(ApplicationDomain.currentDomain);
context.securityDomain = SecurityDomain.currentDomain;
load(re,context);