开发者

Fast way to set text format for a range of Text using TLF

开发者 https://www.devze.com 2022-12-17 06:14 出处:网络
i have to set the text format for some tokens in a plain text. I\'m trying to use the Text Layout Framework to improve the speed of the operation but i\'ve founded that TLF is far slower (10X in my te

i have to set the text format for some tokens in a plain text. I'm trying to use the Text Layout Framework to improve the speed of the operation but i've founded that TLF is far slower (10X in my tests) than the old setTextFormat(). For each token i cal开发者_开发技巧l this function:

public function setTextFormat(format:TextLayoutFormat, begin:int, end:int):void{

            var selection:SelectionState = new SelectionState(this._textFlow, begin, end, this._normalFormat);
            IEditManager(_textFlow.interactionManager).applyLeafFormat(format, selection);

        }

is there any faster and clever way to do this operation ?

Thanks


Most of the processing time in these TLF updates is the recalculation and update of the display. The association of particular formats with portions of your text model is much less intensive. Unfortunately, the applyLeafFormat() call does both format-association and redisplay operations. You need to split those two up.

Instead of only dealing with your tokens in terms of their absolute positions, you could split them into separate FlowElement objects (most likely SpanElements), which can be uniquely identified with an "id" property. Once your tokens are in separate elements, it becomes straightforward to iterate through a whole lot of them, change format characteristics, and only force a display update at the end.

for each (var id:String in ids) {
    var element:SpanElement = _textFlow.getElementByID(id) as SpanElement;
    if (element) {
        element.format = getAppropriateFormatForElement(element);
    }
}
_textFlow.flowComposer.updateAllControllers();

As an aside, splitting your tokens into elements also opens the door for storing your token classification in the elements themselves, freeing you from maintaining a separate classification mapping structure.

0

精彩评论

暂无评论...
验证码 换一张
取 消