Flash: TextCreator

This is class that with which you can create TextField in couple of lines:

1
2
3
4
5
6
7
var text1_txt:TextField = TextCreator.createTextField(TextCreator.FONT_MYRIADPRO_12, 0x0, 11, true, 0, true);
//var text1_txt:TextField= TextCreator.createTextField(TextCreator.FONT_MYRIADPRO_12, 0xffffff, 11, true, -0.3, true, TextCreator.TFA_CENTER);
//var text1_txt:TextField= TextCreator.createTextField(TextCreator.FONT_MYRIADPRO_12, 0x565656, 11);
text1_txt.text = "text1";
text1_txt.x = 93;
text1_txt.y = 20;
addChild(text1_txt);

You have to keep in mind that VerdanaEmbedded and MyRiadPro12Embedded are extended Font classes in package com.stanislavstankov.fonts Example:

1
2
3
4
5
6
7
8
9
10
11
12
13
package com.stanislavstankov.fonts {
    import flash.text.Font;
   
    public final class MyRiadPro12Embedded extends Font {
       
        /*------------------------------------
            Constructor
        ------------------------------------*/

        public function MyRiadPro12Embedded():void {
        }
       
    }
}

Text Creator Class.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package com.stanislavstankov.utils {
    import com.stanislavstankov.fonts.*;
   
    import flash.text.AntiAliasType;
    import flash.text.Font;
    import flash.text.TextField;
    import flash.text.TextFieldAutoSize;
    import flash.text.TextFieldType;
    import flash.text.TextFormat;
    import flash.text.TextFormatAlign;
   
    public final class TextCreator {
       
        public static const FONT_VERDANA:int = 1;
        public static const FONT_MYRIADPRO_12:int = 2;
       
        /**
         * Constant; centers the text in the text field.
         *  Use the syntax TextFormatAlign.CENTER.
         */

        public static const TFA_CENTER:String = "center";
        /**
         * Constant; justifies text within the text field.
         *  Use the syntax TextFormatAlign.JUSTIFY.
         */

        public static const TFA_JUSTIFY:String = "justify";
        /**
         * Constant; aligns text to the left within the text field.
         *  Use the syntax TextFormatAlign.LEFT.
         */

        public static const TFA_LEFT:String = "left";
        /**
         * Constant; aligns text to the right within the text field.
         *  Use the syntax TextFormatAlign.RIGHT.
         */

        public static const TFA_RIGHT:String = "right";
       
        public static function createTextField(font_:int, color_:int, size_:int, kerning_:Boolean = true, letterSpacing_:Number = 0, bold_:Boolean = false, align_:String = TFA_LEFT):TextField {
            var textformat:TextFormat = new TextFormat();
            textformat.align = align_;
            textformat.color = color_;
            textformat.bold = bold_;
            textformat.font = getFont(font_).fontName;
            textformat.size = size_;
            textformat.kerning = kerning_;
            textformat.letterSpacing = letterSpacing_;
           
            var textfield:TextField = new TextField();
            textfield.antiAliasType = AntiAliasType.ADVANCED;
            textfield.embedFonts = textfield.cacheAsBitmap = true;
            textfield.selectable = textfield.mouseEnabled = textfield.mouseWheelEnabled = false;
            textfield.defaultTextFormat = textformat;
            textfield.autoSize = TextFieldAutoSize.LEFT;
            return textfield;
        }
       
       
        private static function getFont($fontID_:int):Font {
            var font:Font;
            switch ($fontID_) {
                case FONT_VERDANA:
                    font = new VerdanaEmbedded();
                break;
                case FONT_MYRIADPRO_12:
                    font = new MyRiadPro12Embedded();
                break;
            }
            return font;
        }
       
        public static function noMoreLine($txt_:TextField, $fromLine_:int, $suffix_:String = "..."):void {
            var imax:int = $txt_.numLines - $fromLine_;
            //added !!! on 28!
            $txt_.height = $txt_.textHeight + $fromLine_ * 2;
            if (imax < 1) {
                return void;
            }
            var numSymb:int = 0;
            for (var i:int = i; i < imax; i++) {
                numSymb += $txt_.getLineLength(i + $fromLine_);
            }
            var txt:String = $txt_.text;
            $txt_.text = txt.substr(0, txt.length - numSymb - $suffix_.length) + $suffix_;
            $txt_.height = $txt_.textHeight + $fromLine_ * 2;
        }
       
    }
}
Creative Commons License
This work, unless otherwise expressly stated, is licensed under a Creative Commons Attribution 2.5 Bulgaria License.

Comments are closed.