From f50613a913a4e666c6ea29e8537896b243022f76 Mon Sep 17 00:00:00 2001 From: Ray Greenwell Date: Fri, 27 Apr 2007 01:27:54 +0000 Subject: [PATCH] Utility methods to attempt to make initializing a TextField easier. git-svn-id: svn+ssh://src.earth.threerings.net/nenya/trunk@213 ed5b42cb-e716-0410-a449-f6a68f950b19 --- src/as/com/threerings/flash/TextFieldUtil.as | 72 ++++++++++++++++++++ 1 file changed, 72 insertions(+) create mode 100644 src/as/com/threerings/flash/TextFieldUtil.as diff --git a/src/as/com/threerings/flash/TextFieldUtil.as b/src/as/com/threerings/flash/TextFieldUtil.as new file mode 100644 index 00000000..22b7bec6 --- /dev/null +++ b/src/as/com/threerings/flash/TextFieldUtil.as @@ -0,0 +1,72 @@ +package com.threerings.flash { + +import flash.filters.GlowFilter; + +import flash.text.TextField; +import flash.text.TextFieldAutoSize; +import flash.text.TextFieldType; +import flash.text.TextFormat; +import flash.text.TextFormatAlign; + +public class TextFieldUtil +{ + /** + * Create a TextField. + * + * args: contains properties with which to initialize the TextField + */ + public static function createField (text :String, args :Object = null) :TextField + { + var tf :TextField = new TextField(); + tf.background = getArg(args, "background", false); + tf.border = getArg(args, "border", false); + tf.multiline = getArg(args, "multiline", false); + tf.type = getArg(args, "type", TextFieldType.DYNAMIC); + + var format :* = getArg(args, "format"); + if (format !== undefined) { + tf.defaultTextFormat = format; + } + + var outColor :* = getArg(args, "outlineColor"); + if (outColor !== undefined) { + tf.filters = [ new GlowFilter(uint(outColor), 1, 2, 2, 255) ]; + } + + tf.autoSize = getArg(args, "autoSize", TextFieldAutoSize.LEFT); + tf.text = text; + if (tf.autoSize != null) { + tf.width = tf.textWidth + 5; + tf.height = tf.textHeight + 4; + } + + return tf; + } + + /** + * Create a TextFormat. + */ + public static function createFormat (args :Object) :TextFormat + { + var f :TextFormat = new TextFormat(); + f.align = getArg(args, "align", TextFormatAlign.LEFT); + f.blockIndent = getArg(args, "blockIndent", null); + f.bold = getArg(args, "bold", false); + f.bullet = getArg(args, "bullet", null); + + f.size = getArg(args, "size", 18); + f.font = getArg(args, "font", "Arial"); + f.color = getArg(args, "color", 0x000000); + + return f; + } + + protected static function getArg (args :Object, name :String, defVal :* = undefined) :* + { + if (args != null && (name in args)) { + return args[name]; + } + return defVal; + } +} +}