针对FlexLite 的DataGroup文本的itemRenderer

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 IItemRenderer    {
 
        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();
            }
        }
    }
}

调用:
var dataGroup:DataGroup = new DataGroup();
dataGroup.dataProvider = new ArrayCollection();
dataGroup.itemRenderer = PacketItemRenderer;

你可能感兴趣的:(itemRenderer)