Actionscript 3 如何在AS3中加载Tlf中的波斯语文本文件?
我有文本文件:File.txt: 1301,嗨,我的闪光灯 3001,سابببببد 在我的代码中,加载文本文件的代码是:Actionscript 3 如何在AS3中加载Tlf中的波斯语文本文件?,actionscript-3,flash,Actionscript 3,Flash,我有文本文件:File.txt: 1301,嗨,我的闪光灯 3001,سابببببد 在我的代码中,加载文本文件的代码是: var-url:URLRequest=newurlrequest(“file.txt”); var n=数量; 变量加载器:URLLoader=新的URLLoader(); loader.load(url); loader.addEventListener(Event.COMPLETE、loaderComplete); 函数加载程序完成(e:事件):无效 { tt.te
var-url:URLRequest=newurlrequest(“file.txt”);
var n=数量;
变量加载器:URLLoader=新的URLLoader();
loader.load(url);
loader.addEventListener(Event.COMPLETE、loaderComplete);
函数加载程序完成(e:事件):无效
{
tt.text=loader.data;
}
问题是数据加载正确(假设.txt文件以unicode格式正确编码。请使用记事本+)但文本字段无法呈现特殊的unicode文本、波斯语、阿拉伯语或希伯来语。
不幸的是,唯一可用的解决方案是利用flashcs5中的“TLFTextField”组件,该组件由于实现笨拙而在以后的版本中被删除。无论如何,虽然有点晚了,但我在这里发布了使用tlf的解决方案,因为使用tlf正确渲染文本可能会非常痛苦,给你:
- 下载“textLayout.swc”和“tlfruntime.swc”
- 将这些SWC添加到项目库中
- 嵌入包含unicode语言符号的字体(本例中为首选的波斯语字体)这一点非常重要,因为并非每台目标机器都有这种字体,即使显示文本,这也足以避免产生抗锯齿文本
- 确保从字体属性的ActionScript选项卡中选择了FTE(DF4)
- 您必须实例化嵌入的字体。在下面的代码中,我们已经完成了这项工作。假设embeddedFont是嵌入字体的类名李>
- 使用以下函数将文本呈现到tlf中(请注意,这不是在flash timeline中编写的代码,它是在单独的编辑器中编写的,由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;
}
private var _tlf:TLFTextField = text("سلام", { textIndent: 8, color: 0xAAAAAA, fontSize: 28, autoSize: "center", antiAliasType: "advanced" } );
stage.addChild(_tlf);
- 使用此代码获取TLFTextField并将其添加到显示:
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;
}
private var _tlf:TLFTextField = text("سلام", { textIndent: 8, color: 0xAAAAAA, fontSize: 28, autoSize: "center", antiAliasType: "advanced" } );
stage.addChild(_tlf);
请注意,根据我的经验,用RTL语言创建tlf文本时有100毫秒的延迟。确保在正确的时间(没有动画、音频或视频的情况下)创建所有文本,否则会出现一个小停顿,足以破坏用户体验。看一看。