/dev/FLDev/lib/com/bit101/components/Label.as

http://github.com/OpenRTMFP/ArcusNode · ActionScript · 154 lines · 78 code · 15 blank · 61 comment · 3 complexity · 97a16c7ffce57dadd4b5f02e455ab4c5 MD5 · raw file

  1. /**
  2. * Label.as
  3. * Keith Peters
  4. * version 0.9.10
  5. *
  6. * A Label component for displaying a single line of text.
  7. *
  8. * Copyright (c) 2011 Keith Peters
  9. *
  10. * Permission is hereby granted, free of charge, to any person obtaining a copy
  11. * of this software and associated documentation files (the "Software"), to deal
  12. * in the Software without restriction, including without limitation the rights
  13. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  14. * copies of the Software, and to permit persons to whom the Software is
  15. * furnished to do so, subject to the following conditions:
  16. *
  17. * The above copyright notice and this permission notice shall be included in
  18. * all copies or substantial portions of the Software.
  19. *
  20. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  21. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  22. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  23. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  24. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  25. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  26. * THE SOFTWARE.
  27. */
  28. package com.bit101.components
  29. {
  30. import flash.display.DisplayObjectContainer;
  31. import flash.events.Event;
  32. import flash.text.TextField;
  33. import flash.text.TextFieldAutoSize;
  34. import flash.text.TextFormat;
  35. [Event(name="resize", type="flash.events.Event")]
  36. public class Label extends Component
  37. {
  38. protected var _autoSize:Boolean = true;
  39. protected var _text:String = "";
  40. protected var _tf:TextField;
  41. /**
  42. * Constructor
  43. * @param parent The parent DisplayObjectContainer on which to add this Label.
  44. * @param xpos The x position to place this component.
  45. * @param ypos The y position to place this component.
  46. * @param text The string to use as the initial text in this component.
  47. */
  48. public function Label(parent:DisplayObjectContainer = null, xpos:Number = 0, ypos:Number = 0, text:String = "")
  49. {
  50. this.text = text;
  51. super(parent, xpos, ypos);
  52. }
  53. /**
  54. * Initializes the component.
  55. */
  56. override protected function init():void
  57. {
  58. super.init();
  59. mouseEnabled = false;
  60. mouseChildren = false;
  61. }
  62. /**
  63. * Creates and adds the child display objects of this component.
  64. */
  65. override protected function addChildren():void
  66. {
  67. _height = 18;
  68. _tf = new TextField();
  69. _tf.height = _height;
  70. _tf.embedFonts = Style.embedFonts;
  71. _tf.selectable = false;
  72. _tf.mouseEnabled = false;
  73. _tf.defaultTextFormat = new TextFormat(Style.fontName, Style.fontSize, Style.LABEL_TEXT);
  74. _tf.text = _text;
  75. addChild(_tf);
  76. draw();
  77. }
  78. ///////////////////////////////////
  79. // public methods
  80. ///////////////////////////////////
  81. /**
  82. * Draws the visual ui of the component.
  83. */
  84. override public function draw():void
  85. {
  86. super.draw();
  87. _tf.text = _text;
  88. if(_autoSize)
  89. {
  90. _tf.autoSize = TextFieldAutoSize.LEFT;
  91. _width = _tf.width;
  92. dispatchEvent(new Event(Event.RESIZE));
  93. }
  94. else
  95. {
  96. _tf.autoSize = TextFieldAutoSize.NONE;
  97. _tf.width = _width;
  98. }
  99. _height = _tf.height = 18;
  100. }
  101. ///////////////////////////////////
  102. // event handlers
  103. ///////////////////////////////////
  104. ///////////////////////////////////
  105. // getter/setters
  106. ///////////////////////////////////
  107. /**
  108. * Gets / sets the text of this Label.
  109. */
  110. public function set text(t:String):void
  111. {
  112. _text = t;
  113. if(_text == null) _text = "";
  114. invalidate();
  115. }
  116. public function get text():String
  117. {
  118. return _text;
  119. }
  120. /**
  121. * Gets / sets whether or not this Label will autosize.
  122. */
  123. public function set autoSize(b:Boolean):void
  124. {
  125. _autoSize = b;
  126. }
  127. public function get autoSize():Boolean
  128. {
  129. return _autoSize;
  130. }
  131. /**
  132. * Gets the internal TextField of the label if you need to do further customization of it.
  133. */
  134. public function get textField():TextField
  135. {
  136. return _tf;
  137. }
  138. }
  139. }