如何在AS3中加载Tlf中的波斯文本文件?

时间:2015-02-01 08:53:48

标签: actionscript-3 flash

我有文本文件:file.txt:

1301,嗨,我的Flash。

3001,سلامبرتوباد

在我的加载文本文件的代码中是:

var url:URLRequest = new URLRequest("file.txt");
var n=Number;
var loader:URLLoader = new URLLoader();
loader.load(url);
loader.addEventListener(Event.COMPLETE, loaderComplete);
function loaderComplete(e:Event):void
{
	tt.text=loader.data;
	
}

但我的结果是:

1301,嗨,我的Flash。

3001,ÓáÇãÈÑÊæÈÏÏ

1 个答案:

答案 0 :(得分:1)

问题是数据加载正确,(假设您的.txt文件以unicode格式正确编码。使用notepad ++来执行此操作)但您的TextField无法呈现特殊的unicode文本,波斯语,阿拉伯语或希伯来语。 遗憾的是,唯一可用的解决方案是利用闪存cs5中存在的“TLFTextField”组件,并且由于笨重的实现而在后续版本中删除。无论如何,虽然它有点晚了,我用tlf发布解决方案cuz使用tlf来正确渲染文本可能真的很痛苦,在这里你去:

  • 下载“textLayout.swc”和“tlfruntime.swc”
  • 将这些SWC添加到项目库中。
  • 嵌入包含unicode语言符号的字体(在这种情况下是您首选的Farsi字体)这非常重要,因为并非每台目标机器都有此字体,这足以得到消除锯齿的文本,即使文字显示。
  • 确保从字体属性的ActionScript选项卡中选择FTE(DF4)
  • 您必须实例化您的嵌入字体。在下面的代码中我们已经完成了这个。假设embeddedFont是嵌入字体的类名。
  • 使用以下函数将文本呈现为tlf(请注意,这不是用flash时间轴编写的代码,它是用单独的编辑器编写的,由flex sdk编译):

import fl.text.TLFTextField;
import flash.text.AntiAliasType;
import flash.text.engine.FontLookup;
import flash.text.Font;
import flash.text.TextFieldAutoSize;
import flash.utils.getDefinitionByName;
import flashx.textLayout.elements.TextFlow;
import flashx.textLayout.formats.TextAlign;
import flashx.textLayout.formats.TextLayoutFormat;
import flashx.textLayout.formats.VerticalAlign;
private var _font:Font= new embeddedFont() as Font;
/**
 * Creates a center registered TLFTextField with the given text
 * @param   text targer text
 * @param   layoutFormat an object containing layoutFormat data. something like { textIndent: 8, color: 0xAAAAAA, fontFamily: "tahoma", fontSize: 28, autoSize: "center", antiAliasType: "advanced" }
 * @param   width width of the TLFTextField, auto set if 0 
 * @param   height height of the TLFTextField, auto set if 0 
 * @return  a center registered TLFTextField with the given text
 */
public function text(text:String,  layoutFormat:Object = null, width:int = 0, height:int = 0):TLFTextField
{
    var _txt:TLFTextField = new TLFTextField();
    var _layoutFormat:TextLayoutFormat = new TextLayoutFormat();
    if (layoutFormat == null) layoutFormat = new Object();
    // creating the textfield
    if (width != 0) _txt.width = width;
    if (height != 0) _txt.height = height;
    _txt.selectable = false;
    _txt.embedFonts = true;
    _txt.multiline = true;
    //if either width or height are 0(not passed to function) and the wordWrap is true, autoSize wont work and width or height will be 0
    if (width != 0 && height != 0) _txt.wordWrap = true;
    if (layoutFormat.backgroud != undefined) _txt.background = layoutFormat.backgroud;
    if (layoutFormat.backgroundColor != undefined) _txt.backgroundColor = layoutFormat.backgroundColor;
    _txt.autoSize = (layoutFormat.autoSize != undefined)?(layoutFormat.autoSize) : (TextFieldAutoSize.CENTER);
    _txt.verticalAlign=(layoutFormat.verticalAlign != undefined)?(layoutFormat.verticalAlign) : (VerticalAlign.MIDDLE)
    _txt.antiAliasType = (layoutFormat.antiAliasType != undefined)?(layoutFormat.antiAliasType) : (AntiAliasType.ADVANCED);
    // creating layout format
    _layoutFormat.textAlign = (layoutFormat.textAlign != undefined)?(layoutFormat.textAlign):(TextAlign.CENTER);
    _layoutFormat.textIndent = (layoutFormat.textIndent != undefined)?(layoutFormat.textIndent) : (8);
    _layoutFormat.fontLookup = FontLookup.EMBEDDED_CFF;
    _layoutFormat.color = (layoutFormat.color != undefined)?(layoutFormat.color) : (0xFFFFFF);
    _layoutFormat.fontFamily = (layoutFormat.fontFamily != undefined)?(layoutFormat.fontFamily) : (_font.fontName);
    _layoutFormat.fontSize = (layoutFormat.fontSize != undefined)?(layoutFormat.fontSize) : (42);
    //setting text flow, text and attaching layout format
    var _textFlow:TextFlow = _txt.textFlow;
    _textFlow.hostFormat = _layoutFormat;
    _textFlow.flowComposer.updateAllControllers();
    _txt.text = text;
    return _txt;
}

import fl.text.TLFTextField; import flash.text.AntiAliasType; import flash.text.engine.FontLookup; import flash.text.Font; import flash.text.TextFieldAutoSize; import flash.utils.getDefinitionByName; import flashx.textLayout.elements.TextFlow; import flashx.textLayout.formats.TextAlign; import flashx.textLayout.formats.TextLayoutFormat; import flashx.textLayout.formats.VerticalAlign; private var _font:Font= new embeddedFont() as Font; /** * Creates a center registered TLFTextField with the given text * @param text targer text * @param layoutFormat an object containing layoutFormat data. something like { textIndent: 8, color: 0xAAAAAA, fontFamily: "tahoma", fontSize: 28, autoSize: "center", antiAliasType: "advanced" } * @param width width of the TLFTextField, auto set if 0 * @param height height of the TLFTextField, auto set if 0 * @return a center registered TLFTextField with the given text */ public function text(text:String, layoutFormat:Object = null, width:int = 0, height:int = 0):TLFTextField { var _txt:TLFTextField = new TLFTextField(); var _layoutFormat:TextLayoutFormat = new TextLayoutFormat(); if (layoutFormat == null) layoutFormat = new Object(); // creating the textfield if (width != 0) _txt.width = width; if (height != 0) _txt.height = height; _txt.selectable = false; _txt.embedFonts = true; _txt.multiline = true; //if either width or height are 0(not passed to function) and the wordWrap is true, autoSize wont work and width or height will be 0 if (width != 0 && height != 0) _txt.wordWrap = true; if (layoutFormat.backgroud != undefined) _txt.background = layoutFormat.backgroud; if (layoutFormat.backgroundColor != undefined) _txt.backgroundColor = layoutFormat.backgroundColor; _txt.autoSize = (layoutFormat.autoSize != undefined)?(layoutFormat.autoSize) : (TextFieldAutoSize.CENTER); _txt.verticalAlign=(layoutFormat.verticalAlign != undefined)?(layoutFormat.verticalAlign) : (VerticalAlign.MIDDLE) _txt.antiAliasType = (layoutFormat.antiAliasType != undefined)?(layoutFormat.antiAliasType) : (AntiAliasType.ADVANCED); // creating layout format _layoutFormat.textAlign = (layoutFormat.textAlign != undefined)?(layoutFormat.textAlign):(TextAlign.CENTER); _layoutFormat.textIndent = (layoutFormat.textIndent != undefined)?(layoutFormat.textIndent) : (8); _layoutFormat.fontLookup = FontLookup.EMBEDDED_CFF; _layoutFormat.color = (layoutFormat.color != undefined)?(layoutFormat.color) : (0xFFFFFF); _layoutFormat.fontFamily = (layoutFormat.fontFamily != undefined)?(layoutFormat.fontFamily) : (_font.fontName); _layoutFormat.fontSize = (layoutFormat.fontSize != undefined)?(layoutFormat.fontSize) : (42); //setting text flow, text and attaching layout format var _textFlow:TextFlow = _txt.textFlow; _textFlow.hostFormat = _layoutFormat; _textFlow.flowComposer.updateAllControllers(); _txt.text = text; return _txt; }

  • 使用此代码获取TLFTextField并将其添加到display:

private var _tlf:TLFTextField = text("سلام", { textIndent: 8, color: 0xAAAAAA, fontSize: 28, autoSize: "center", antiAliasType: "advanced" } );
stage.addChild(_tlf);

请注意,根据我的经验,在RTL语言中创建tlf文本时会有100毫秒的延迟。确保你在合适的时间内创建所有文本(当没有动画,音频或视频打开时)或者你得到足够的停顿以破坏你的用户体验。