1

我正在尝试编写一个函数,将 TextFormats 应用于它们所属的 TextFields。问题是,我无法将其放入正确的“表格”(如果甚至有正确的表格)。

我的功能目前看起来像这样

function applyFormat(TextFild:String,Format:String,EmbFont:String,NameInJson:String){

//
//example:applyFormat("myTextField","myTextFormat","myFont","TextStuff")
//  
//myData=json object
//all textfields etc. already exist. Just anyone wants to ask if they've been already generated.

//at first i thought it could be as easy as this
this[Format].font=myData.NameInJson.Font //doesn't work obviously

//then I tried this method
this[Format].font=myData.this[NameInJson].Font //doesn't work either

//couldn't find a real solution for this on the internet...    
//how it should turn out in the end: myTextFormat.font=myData.TextStuff.Font
}

myData 的相关部分如下所示:

"TextStuff":
{
"Font":"Arial",
"Size" : "16",
"Bold" : "true",
"Color" : "0xFFFFFF"
}

我是否需要更改我的 Json 文件才能做到这一点?我是否忽略了一种处理此类变量/值的方法?

4

1 回答 1

1

this[Format]将在类中查找名为 Format 的属性。AFAIK 这个类必须是动态的才能工作。

如果您将TextFormat实例保存在该类的对象中,则管理起来会更容易:

// this is a property in the class
private var _textFormats:Object = {};

// create and store text formats in the object
var textFormat:TextFormat = new TextFormat();
_textFormats["myTextFormat"] = textFormat;

现在假设你得到一个这样的 JSON 文本字符串:

{
"TextStuff":{
    "Font":"Arial",
    "Size" : "16",
    "Bold" : "true",
    "Color" : "0xFFFFFF"
  }
}

以下应与此调用一起使用applyFormat("myTextField","myTextFormat","myFont","TextStuff")

function applyFormat(TextField:String, Format:String, EmbFont:String, NameInJson:String):void
{
    var myData:Object = JSON.parse(theJSonString);
    _textFormats[Format].font = myData[NameInJson].Font;
}
于 2013-03-06T17:27:52.220 回答