Flex数据组的文本项呈现器

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Flex数据组的文本项呈现器相关的知识,希望对你有一定的参考价值。

  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. }

以上是关于Flex数据组的文本项呈现器的主要内容,如果未能解决你的问题,请参考以下文章

用于分区列表的Flex Mobile项呈现器

修改 HTML 的文本区域 - 呈现换行符

flex中的组件以呈现excel

Flex:组合框控件的自定义项目渲染器截断文本

如何更改 flex 4 spark 数据组的一个成员的 itemRenderer 的状态?

从 textarea flex3 中保存空格和换行符