Posted By

kuril on 07/05/11


Tagged

Flex ItemRenderer TLF


Versions (?)

Text ItemRenderer for Flex DataGroup


 / Published in: ActionScript 3
 

  1. package
  2. {
  3.  
  4.  
  5. import flash.display.Sprite;
  6.  
  7. import flashx.textLayout.container.ContainerController;
  8. import flashx.textLayout.edit.SelectionManager;
  9. import flashx.textLayout.elements.TextFlow;
  10. import flashx.textLayout.events.CompositionCompleteEvent;
  11.  
  12. import mx.core.IDataRenderer;
  13. import mx.core.UIComponent;
  14.  
  15. import spark.utils.TextFlowUtil;
  16.  
  17. /**
  18.   * Simple TLF based Text ItemRenderer for flex DataGroup
  19.   * After data set and text composed it ivokes invalidateParentSizeAndDisplayList if
  20.   * if height of text container changed.
  21.   */
  22. public class TextItemRenderer extends UIComponent implements IDataRenderer
  23. {
  24.  
  25. private var _container:Sprite;
  26. private var _controller:ContainerController;
  27. private var _textFlow:TextFlow;
  28. private var _selectionManager:SelectionManager;
  29.  
  30. private var dataChanged:Boolean;
  31. private var _data:String;
  32. public function set data(value:Object):void
  33. {
  34. if (_data != value)
  35. {
  36. _data = String(value);
  37. dataChanged = true;
  38. invalidateProperties();
  39. }
  40. }
  41.  
  42. public function get data():Object
  43. {
  44. return _data;
  45. }
  46.  
  47.  
  48. protected override function createChildren():void
  49. {
  50. _container = new Sprite();
  51. _controller = new ContainerController(_container, NaN, NaN);
  52. _selectionManager = new SelectionManager;
  53. addChild(_container);
  54. }
  55.  
  56.  
  57. override protected function commitProperties():void
  58. {
  59. super.commitProperties();
  60. if (dataChanged)
  61. {
  62. if (_textFlow != null)
  63. {
  64. _textFlow.flowComposer.removeAllControllers();
  65.  
  66. _textFlow.removeEventListener(CompositionCompleteEvent.COMPOSITION_COMPLETE,
  67. compositionCompleteHandler);
  68. }
  69. _textFlow = TextFlowUtil.importFromString(_data);
  70.  
  71. _textFlow.addEventListener(CompositionCompleteEvent.COMPOSITION_COMPLETE,
  72. compositionCompleteHandler);
  73. _textFlow.interactionManager = _selectionManager;
  74. _textFlow.flowComposer.addController(_controller);
  75. _textFlow.flowComposer.updateToController();
  76. dataChanged = false;
  77. }
  78. }
  79.  
  80.  
  81. private function
  82. compositionCompleteHandler(event:CompositionCompleteEvent):void
  83. {
  84. if (measuredHeight !=
  85. Math.ceil(_controller.getContentBounds().height))
  86. {
  87. invalidateSize();
  88. invalidateDisplayList();
  89. invalidateParentSizeAndDisplayList();
  90. }
  91. }
  92.  
  93. override protected function measure():void
  94. {
  95. super.measure();
  96. measuredHeight = Math.ceil(_controller.getContentBounds().height);
  97. }
  98.  
  99.  
  100. override protected function updateDisplayList(unscaledWidth:Number, unscaledHeight:Number):void
  101. {
  102. if (unscaledWidth == 0 || unscaledHeight == 0)
  103. {
  104. return;
  105. }
  106. if (_textFlow != null)
  107. {
  108. _controller.setCompositionSize(unscaledWidth, unscaledHeight);
  109. _textFlow.flowComposer.updateToController();
  110. }
  111. }
  112. }
  113. }

Report this snippet  

You need to login to post a comment.