我有文本文件: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,ÓáÇãÈÑÊæÈÏÏ
答案 0 :(得分:1)
问题是数据加载正确,(假设您的.txt文件以unicode格式正确编码。使用notepad ++来执行此操作)但您的TextField无法呈现特殊的unicode文本,波斯语,阿拉伯语或希伯来语。 遗憾的是,唯一可用的解决方案是利用闪存cs5中存在的“TLFTextField”组件,并且由于笨重的实现而在后续版本中删除。无论如何,虽然它有点晚了,我用tlf发布解决方案cuz使用tlf来正确渲染文本可能真的很痛苦,在这里你去:
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;
}
private var _tlf:TLFTextField = text("سلام", { textIndent: 8, color: 0xAAAAAA, fontSize: 28, autoSize: "center", antiAliasType: "advanced" } );
stage.addChild(_tlf);
请注意,根据我的经验,在RTL语言中创建tlf文本时会有100毫秒的延迟。确保你在合适的时间内创建所有文本(当没有动画,音频或视频打开时)或者你得到足够的停顿以破坏你的用户体验。