你的問題有點難以理解,但這就是我所做的:你有一個固定數量的SpanElements,你希望用一個數組中的文本替換每個Span的文本長度等於斯潘斯的數量)。如果我錯了,請糾正我。
此外,從您的代碼我假設每個Span都有自己的段落。有一點是這樣的:
<s:TextFlow id="mytextflow">
<s:p><s:span>old A</s:span></s:p>
<s:p><s:span>old B</s:span></s:p>
</s:TextFlow>
,你想從例如值替換:
var line_text:Array = ["new A", "new B"];
首先,我會告訴你怎麼做,然後我會告訴你在哪裏你代碼是錯誤的。
解決方案
var textLines:Array = ["new A", "new B"];
//we know all children of mytextflow are ParagraphElements
var paragraphs:Array = mytextflow.mxmlChildren;
var numParagraphs:int = paragraphs.length;
//loop over the ParagraphElements
for (var i:int=0; i<numParagraphs; i++) {
//we know the ParagraphElement only contains one SpanElement,
//so we can get it through getFirstLeaf() and cast it as a SpanElement
var span:SpanElement = paragraphs[i].getFirstLeaf() as SpanElement;
//if it really is a SpanElement, we can set its 'text' property
if (span) span.text = textLines[i];
}
如果您TextFlow對象更比compicated,還有通過所有的片式元件的另一種方式循環,但它是稍微複雜一些。
現在對於什麼是你的代碼錯誤:
首先,實例化一個新的SpanElement中和新的ParagraphElement中。然後在接下來的2行中,您只需覆蓋這些變量。這完全沒有必要。這就像寫作:var s:String = "a"; s = "b";
你試圖通過findLeaf(ii)
找到跨度。必須傳遞給「findLeaf」的參數是要查找包含此字符的SpanElement的文本中某個特定字符的索引。不是 - 你似乎認爲 - TextFlow對象中的SpanElement的某種索引。
- 您重新添加跨度到段落。它不會破壞你的代碼,但它是完全多餘的。所有你需要做的就是替換'文本'屬性。
- 您在循環中每次傳遞時都會調用
updateAllControllers()
。這也是多餘的,會導致更大文本的性能問題。在所有文本被替換之後,只需調用一次就足夠了(我認爲你根本不需要調用它,因爲你並沒有真正改變佈局)。
非常匹配 – user836080
但它是nead mytextflow.flowComposer.updateAllControllers(); – user836080