Flex数据组的文本项呈现器
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Flex数据组的文本项呈现器相关的知识,希望对你有一定的参考价值。
package { import flash.display.Sprite; import flashx.textLayout.container.ContainerController; import flashx.textLayout.edit.SelectionManager; import flashx.textLayout.elements.TextFlow; import flashx.textLayout.events.CompositionCompleteEvent; import mx.core.IDataRenderer; import mx.core.UIComponent; import spark.utils.TextFlowUtil; /** * Simple TLF based Text ItemRenderer for flex DataGroup * After data set and text composed it ivokes invalidateParentSizeAndDisplayList if * if height of text container changed. */ public class TextItemRenderer extends UIComponent implements IDataRenderer { private var _container:Sprite; private var _controller:ContainerController; private var _textFlow:TextFlow; private var _selectionManager:SelectionManager; private var dataChanged:Boolean; private var _data:String; public function set data(value:Object):void { if (_data != value) { _data = String(value); dataChanged = true; invalidateProperties(); } } public function get data():Object { return _data; } protected override function createChildren():void { _container = new Sprite(); _controller = new ContainerController(_container, NaN, NaN); _selectionManager = new SelectionManager; addChild(_container); } override protected function commitProperties():void { super.commitProperties(); if (dataChanged) { if (_textFlow != null) { _textFlow.flowComposer.removeAllControllers(); _textFlow.removeEventListener(CompositionCompleteEvent.COMPOSITION_COMPLETE, compositionCompleteHandler); } _textFlow = TextFlowUtil.importFromString(_data); _textFlow.addEventListener(CompositionCompleteEvent.COMPOSITION_COMPLETE, compositionCompleteHandler); _textFlow.interactionManager = _selectionManager; _textFlow.flowComposer.addController(_controller); _textFlow.flowComposer.updateToController(); dataChanged = false; } } private function compositionCompleteHandler(event:CompositionCompleteEvent):void { if (measuredHeight != Math.ceil(_controller.getContentBounds().height)) { invalidateSize(); invalidateDisplayList(); invalidateParentSizeAndDisplayList(); } } override protected function measure():void { super.measure(); measuredHeight = Math.ceil(_controller.getContentBounds().height); } override protected function updateDisplayList(unscaledWidth:Number, unscaledHeight:Number):void { if (unscaledWidth == 0 || unscaledHeight == 0) { return; } if (_textFlow != null) { _controller.setCompositionSize(unscaledWidth, unscaledHeight); _textFlow.flowComposer.updateToController(); } } } }
以上是关于Flex数据组的文本项呈现器的主要内容,如果未能解决你的问题,请参考以下文章