Posted By

Wiederkehr on 01/05/09


Tagged

text as3


Versions (?)

Who likes this?

3 people have marked this snippet as a favorite

Meestercor
thastyle
iamgowan


TextHelper


 / Published in: ActionScript 3
 

  1. //////////////////////////////////////////////////////////////////////////
  2. // TextHelper
  3. //
  4. // Created by Benjamin Wiederkehr on 2009-01-04.
  5. // Copyright (c) 2009 Benjamin Wiederkehr / Artillery.ch. All rights reserved.
  6. //
  7. //////////////////////////////////////////////////////////////////////////
  8. package {
  9. //--------------------------------------
  10. // IMPORT
  11. //--------------------------------------
  12. import flash.events.*;
  13. import flash.text.*;
  14. [Embed(source='fonts/FontName.otf', fontName="FontName")]
  15. /**
  16. * TextHelper
  17. *
  18. * @langversion ActionScript 3.0
  19. * @playerversion Flash 9.0
  20. * @author Benjamin Wiederkehr
  21. * @since 2009-01-04
  22. * @version 0.1
  23. */
  24. public class TextHelper extends EventDispatcher {
  25. //--------------------------------------
  26. // VARIABLES
  27. //--------------------------------------
  28. private static var _instance : TextHelper;
  29. //--------------------------------------
  30. // CONSTANTS
  31. //--------------------------------------
  32. private const FONT :String = 'FontName';
  33. private const T_COLOR :uint = 0xFFFFFF;
  34. private const T_SIZE :uint = 16;
  35. //--------------------------------------
  36. // SINGLETON CONSTRUCTION
  37. //--------------------------------------
  38. public static function getInstance() : TextHelper {
  39. return initialize();
  40. } // END getInstance()
  41. public static function initialize() : TextHelper {
  42. if (_instance == null){
  43. _instance = new TextHelper();
  44. };
  45. return _instance;
  46. } // END initialize()
  47. public function TextHelper(){
  48. super();
  49. if( _instance != null ) throw new Error( "Error: TextHelper already initialised." );
  50. if( _instance == null ) _instance = this;
  51. } // END TextHelper()
  52. //--------------------------------------
  53. // PUBLIC METHODS
  54. //--------------------------------------
  55. public function createField(_text:String = 'new TextField'):TextField{
  56. var tf:TextField = new TextField();
  57. tf.embedFonts = true;
  58. tf.antiAliasType = AntiAliasType.ADVANCED;
  59. tf.defaultTextFormat = createFormat();
  60. tf.text = _text;
  61. tf.width = tf.textWidth + T_SIZE / 2;
  62. return tf;
  63. } // END createField()
  64. private function createFormat(_font:String = FONT, _color:uint = T_COLOR, _size:uint = T_SIZE):TextFormat{
  65. var tFormat:TextFormat = new TextFormat();
  66. tFormat.font = _font;
  67. tFormat.color = _color;
  68. tFormat.size = _size;
  69. return tFormat;
  70. } // END createFormat()
  71. public function formatText(_tf:TextField, _color:uint = T_COLOR, _size:uint = T_SIZE):void {
  72. _tf.setTextFormat(createFormat(FONT, _color, _size));
  73. } // END formatText()
  74. } // END TextHelper Class
  75. } // END package

Report this snippet  

You need to login to post a comment.