/ Published in: ActionScript 3
Expand |
Embed | Plain Text
////////////////////////////////////////////////////////////////////////// // TextHelper // // Created by Benjamin Wiederkehr on 2009-01-04. // Copyright (c) 2009 Benjamin Wiederkehr / Artillery.ch. All rights reserved. // ////////////////////////////////////////////////////////////////////////// package { //-------------------------------------- // IMPORT //-------------------------------------- import flash.events.*; import flash.text.*; [Embed(source='fonts/FontName.otf', fontName="FontName")] /** * TextHelper * * @langversion ActionScript 3.0 * @playerversion Flash 9.0 * @author Benjamin Wiederkehr * @since 2009-01-04 * @version 0.1 */ public class TextHelper extends EventDispatcher { //-------------------------------------- // VARIABLES //-------------------------------------- private static var _instance : TextHelper; //-------------------------------------- // CONSTANTS //-------------------------------------- private const FONT :String = 'FontName'; private const T_COLOR :uint = 0xFFFFFF; private const T_SIZE :uint = 16; //-------------------------------------- // SINGLETON CONSTRUCTION //-------------------------------------- public static function getInstance() : TextHelper { return initialize(); } // END getInstance() public static function initialize() : TextHelper { if (_instance == null){ _instance = new TextHelper(); }; return _instance; } // END initialize() public function TextHelper(){ super(); if( _instance != null ) throw new Error( "Error: TextHelper already initialised." ); if( _instance == null ) _instance = this; } // END TextHelper() //-------------------------------------- // PUBLIC METHODS //-------------------------------------- public function createField(_text:String = 'new TextField'):TextField{ var tf:TextField = new TextField(); tf.embedFonts = true; tf.antiAliasType = AntiAliasType.ADVANCED; tf.defaultTextFormat = createFormat(); tf.text = _text; tf.width = tf.textWidth + T_SIZE / 2; return tf; } // END createField() private function createFormat(_font:String = FONT, _color:uint = T_COLOR, _size:uint = T_SIZE):TextFormat{ var tFormat:TextFormat = new TextFormat(); tFormat.font = _font; tFormat.color = _color; tFormat.size = _size; return tFormat; } // END createFormat() public function formatText(_tf:TextField, _color:uint = T_COLOR, _size:uint = T_SIZE):void { _tf.setTextFormat(createFormat(FONT, _color, _size)); } // END formatText() } // END TextHelper Class } // END package
You need to login to post a comment.
