SomaText

SomaText is a standalone version of the class you can find in the Soma framework. SomaText extends The Flash built-in class TextField and add powerful features, the main purpose is be able to easily add styles to the text field.

SomaText has other features such as default values, default stylesheet, styles. SomaText is able to parse a stylesheet to apply TextField and TextFormat properties straight from the stylesheet.

You will be fine with SomaText as it is extending the TextField class and SomaText also shorten the syntax when you create instances.

This work is licenced under a Mozilla Public License 1.1 (MPL 1.1)

Version 1.0.1

docs

download source

Actionscript:
  1. package com.soundstep.utils {
  2.    
  3.     import flash.text.StyleSheet;
  4.     import flash.text.TextField;
  5.     import flash.text.TextFieldType;
  6.     import flash.text.TextFormat;
  7.     import flash.utils.describeType;   
  8.    
  9.     /**
  10.      * <b>Author:</b> Romuald Quantin - <a href="http://www.soundstep.com/" target="_blank">www.soundstep.com</a><br />
  11.      * <b>Class version:</b> 1.0.1<br />
  12.      * <b>Actionscript version:</b> 3.0<br />
  13.      * <b>Copyright:</b>
  14.      * Mozilla Public License 1.1 (MPL 1.1)<br />
  15.      * <a href="http://www.opensource.org/licenses/mozilla1.1.php" target="_blank">http://www.opensource.org/licenses/mozilla1.1.php</a><br />
  16.      * <b>Date:</b> 01-2009<br />
  17.      * <b>Usage:</b>
  18.      * SomaText is extending the TextField class and add a powerful Stylesheet Management with parsable properties.<br />
  19.      * SomaText has been extracted from the <a href="http://www.soundstep.com/somaprotest/" target="_blank">Soma MVC Framework</a> to be used as a standalone (this version is slightly different).<br />
  20.      * You can find a full list of CSS properties you can use with SomaText in the Soma Protest <a href="http://www.soundstep.com/somaprotest/www/#/stylesheet/css-properties/" target="_blank">CSS properties page</a>.
  21.      * @example
  22.      * <listing version="3.0">
  23.      * SomaText.globalStyleSheet = _globalStylesheetLoaded;
  24.      * // text
  25.      * var s1:SomaText = new SomaText("simple text");
  26.      * addChild(s1);
  27.      * // text with style from the global stylesheet
  28.      * var s2:SomaText = new SomaText("text with style from the global stylesheet", "simpleStyle");
  29.      * addChild(s2);
  30.      * // text with style from another stylesheet
  31.      * var s3:SomaText = new SomaText("text with style from another stylesheet", "simpleStyle", _customStylesheet);
  32.      * addChild(s3);
  33.      * // multiline text with properties set in actionscript
  34.      * var s4:SomaText = new SomaText("multiline text with textfield properties modified in the actionscript: width, multiline, wordWrap, selectable, and so on.", "defaultText");
  35.      * s4.width = 220;
  36.      * s4.multiline = true;
  37.      * s4.wordWrap = true;
  38.      * s4.selectable = true;
  39.      * // can be set in the constructor or using the setProperties method:
  40.      * //s4.setProperties({width: 220, multiline: true, wordWrap: true, selectable: true});
  41.      * addChild(s4);
  42.      * // multiline text with properties set in the stylesheet
  43.      * var s5:SomaText = new SomaText("multiline text with textfield properties set in the stylesheet: width, multiline, word-wrap, selectable, and so on.", "defaultTextMultiline");
  44.      * addChild(s5);
  45.      * // multiline html text
  46.      * var p1:String = '&lt;p&gt;paragraph 1, &lt;b&gt;multiline html text&lt;/b&gt; with &lt;i&gt;more style&lt;/i&gt; in the &lt;span class="color1"&gt;same SomaText&lt;/span&gt; instance.&lt;/p&gt;';
  47.      * var p2:String = '&lt;p class="para"&gt;paragraph 2, &lt;b&gt;multiline html text&lt;/b&gt; with &lt;i&gt;more style&lt;/i&gt; in the &lt;span class="color1"&gt;same SomaText&lt;/span&gt; instance.&lt;/p&gt;';
  48.      * var s6:SomaText = new SomaText(p1 + p2, "defaultTextMultiline");
  49.      * addChild(s6);
  50.      * // text input
  51.      * var s7:SomaText = new SomaText("text input", "defaultTextMultiline");
  52.      * s7.type = TextFieldType.INPUT;
  53.      * s7.autoSize = TextFieldAutoSize.NONE;
  54.      * s7.width = 220;
  55.      * s7.height = 20;
  56.      * s7.background = true;
  57.      * s7.backgroundColor = 0xFFFFFF;
  58.      * addChild(s7);
  59.      * // text with link
  60.      * var s8:SomaText = new SomaText('text with <a href="http://www.soundstep.com/somaprotest/www/#/stylesheet/" target="_blank">link and hover</a>', "defaultText");
  61.      * addChild(s8);
  62.      * </listing>
  63.      * @inheritDoc
  64.      */
  65.    
  66.     public class SomaText extends TextField {
  67.  
  68.         //------------------------------------
  69.         // private, protected properties
  70.         //------------------------------------
  71.        
  72.         // global stylesheet used as a default
  73.         private static var _globalStyleSheet:StyleSheet;
  74.         // current style 
  75.         private var _style:String;
  76.         // current stylesheet
  77.         private var _stylesheet:StyleSheet;
  78.         // store the defaultTextFormat of the TextField
  79.         private var _defaultTextFormat:TextFormat;
  80.         // store the default properties of the TextField
  81.         private var _defaultProperties:Array;
  82.         // store the default values of the TextField
  83.         private var _defaultValues:Array;
  84.         // error message if the global stylesheet is missing
  85.         private var _errorGlobalStylesheet:String = "Error in SomaText, the global Stylesheet must be specified using SomaText.globalStyleSheet = myGlobalStyleSheet";
  86.        
  87.         //------------------------------------
  88.         // public properties
  89.         //------------------------------------
  90.        
  91.         /**
  92.          * default type that will be used for each new instance created (TextFieldType)
  93.          */
  94.         public static var DEFAULT_TYPE:String = "dynamic";
  95.         /**
  96.          * default font embedded value that will be used for each new instance created
  97.          */
  98.         public static var DEFAULT_EMBED_FONT:Boolean = false;
  99.         /**
  100.          * default alias that will be used for each new instance created (AntiAliasType)
  101.          */
  102.         public static var DEFAULT_ANTIALIAS:String = "advanced";
  103.         /**
  104.          * default autosize that will be used for each new instance created (TextFieldAutoSize)
  105.          */
  106.         public static var DEFAULT_AUTOSIZE:String = "left";
  107.         /**
  108.          * default multiline value that will be used for each new instance created
  109.          */
  110.         public static var DEFAULT_MULTILINE:Boolean = false;
  111.         /**
  112.          * default wordWrap value that will be used for each new instance created
  113.          */
  114.         public static var DEFAULT_WORDWRAP:Boolean = false;
  115.         /**
  116.          * default selectable value that will be used for each new instance created
  117.          */
  118.         public static var DEFAULT_SELECTABLE:Boolean = false;
  119.         /**
  120.          * default condenseWhite value that will be used for each new instance created
  121.          */
  122.         public static var DEFAULT_CONDENSE_WHITE:Boolean = true;
  123.         /**
  124.          * default mouseWheelEnabled value that will be used for each new instance created
  125.          */
  126.         public static var DEFAULT_MOUSEWHEEL_ENABLED:Boolean = false;
  127.         /**
  128.          * default doubleClickEnabled value that will be used for each new instance created
  129.          */
  130.         public static var DEFAULT_DOUBLECLICK_ENABLED:Boolean = false;
  131.         /**
  132.          * default gridFitType value that will be used for each new instance created (GridFitType)
  133.          */
  134.         public static var DEFAULT_GRIDFITTYPE:String = "none";
  135.         /**
  136.          * default useRichTextClipboard value that will be used for each new instance created
  137.          */
  138.         public static var DEFAULT_RICHTEXT_CLIPBOARD_ENABLED:Boolean = true;
  139.        
  140.         //------------------------------------
  141.         // constructor
  142.         //------------------------------------
  143.        
  144.         /**
  145.          * create a new instance of the SomaText class
  146.          * @param text or htmlText value
  147.          * @param style of a stylesheet (without dot), you must register a global stylesheet (SomaText.globalStyleSheet) or pass a stylesheet in the third parameter
  148.          * @param object of properties, shortcut to set properties to the TextField, example: {multiline: true, selectable: true}
  149.          */
  150.         public function SomaText(value:String = "", style:String = null, stylesheet:StyleSheet = null, properties:Object = null) {
  151.             super();
  152.             init(value, style, stylesheet, properties);
  153.         }
  154.  
  155.         //
  156.         // PRIVATE, PROTECTED
  157.         //________________________________________________________________________________________________
  158.        
  159.         /** @private */
  160.         // change a string to a Boolean (when styles are parsed)
  161.         private function stringToBoolean(value:String):Boolean {
  162.             return (value.toLowerCase() == "true" || value.toLowerCase() == "1");
  163.         }
  164.        
  165.         /** @private */
  166.         // change a string to a Boolean (when styles are parsed)
  167.         private function getDefaultStyle(style:String):Object {
  168.             var ds:StyleSheet = _globalStyleSheet;
  169.             if (ds == null) throw new Error(_errorGlobalStylesheet);
  170.             return ds.getStyle(style);
  171.         }
  172.        
  173.         /** @private */
  174.         // initialize the TextField
  175.         protected function init(value:String = "", style:String = null, stylesheet:StyleSheet = null, properties:Object = null):void {
  176.             setDefault();
  177.             memorizeProperties();
  178.             _stylesheet = stylesheet;
  179.             // apply style
  180.             if (style != null) applyStyle(style);
  181.             // apply stylesheet
  182.             if (style != null || stylesheet != null) {
  183.                 var currentStylesheet:StyleSheet = (_stylesheet != null) ? _stylesheet: _globalStyleSheet;
  184.                 _stylesheet = currentStylesheet;
  185.                 styleSheet = _stylesheet;
  186.             }
  187.             if (properties != null) setProperties(properties);
  188.             if (value.indexOf("<") != -1 && value.indexOf(">") != -1) {
  189.                 htmlText = value;
  190.             }
  191.             else {
  192.                 text = value;
  193.             }
  194.         }
  195.        
  196.         /** @private */
  197.         // memorize the default properties to reset the style of the TextField
  198.         protected function memorizeProperties():void {
  199.             _defaultTextFormat = defaultTextFormat;
  200.             _defaultProperties = [];
  201.             _defaultValues = [];
  202.             var listProperties:XMLList = describeType(this)..*.(name() == "accessor" && @access == "readwrite" && @declaredBy == "flash.text::TextField");
  203.             for (var i:int=0; i<listProperties.length(); i++) {
  204.                 _defaultProperties.push(listProperties[i].@name);
  205.                 _defaultValues.push(this[listProperties[i].@name]);
  206.             }
  207.         }
  208.        
  209.         /** @private */
  210.         // set default properties from global values
  211.         protected function setDefault():void {
  212.             type = SomaText.DEFAULT_TYPE;
  213.             embedFonts = SomaText.DEFAULT_EMBED_FONT;
  214.             antiAliasType = SomaText.DEFAULT_ANTIALIAS;
  215.             autoSize = SomaText.DEFAULT_AUTOSIZE;
  216.             multiline = SomaText.DEFAULT_MULTILINE;
  217.             wordWrap = SomaText.DEFAULT_WORDWRAP;
  218.             selectable = SomaText.DEFAULT_SELECTABLE;
  219.             condenseWhite = SomaText.DEFAULT_CONDENSE_WHITE;
  220.             mouseWheelEnabled = SomaText.DEFAULT_MOUSEWHEEL_ENABLED;
  221.             doubleClickEnabled = SomaText.DEFAULT_DOUBLECLICK_ENABLED;
  222.             gridFitType = SomaText.DEFAULT_GRIDFITTYPE;
  223.             useRichTextClipboard = SomaText.DEFAULT_RICHTEXT_CLIPBOARD_ENABLED;
  224.         }
  225.        
  226.         /** @private */
  227.         // parse an object and apply the properties to the TextField
  228.         protected function applyTextFieldValues(obj:Object):void {
  229.             for (var prop:String in obj) {
  230.                 if (this.hasOwnProperty(prop)) {
  231.                     if (this[prop] is Boolean) {
  232.                         obj[prop] = stringToBoolean(obj[prop]);
  233.                     }
  234.                     else if (prop == "backgroundColor" || prop == "borderColor") {
  235.                         obj[prop] = parseInt(String(obj[prop]).substring(1), 16);
  236.                     }
  237.                     else if (this[prop] is int) {
  238.                         obj[prop] = parseInt(obj[prop]);
  239.                     }
  240.                     // specific actions
  241.                     if (prop == "alpha" || prop == "scaleX" || prop == "scaleY") {
  242.                         obj[prop] *= .01;
  243.                     }
  244.                     // apply
  245.                     this[prop] = obj[prop];
  246.                 }
  247.             }
  248.         }
  249.        
  250.         /** @private */
  251.         // apply the style to the TextField
  252.         protected function applyStyle(style:String):void {
  253.             var currentStylesheet:StyleSheet;
  254.             var styleTarget:Object;
  255.             if (_stylesheet == null) {
  256.                 // unspecified stylesheet
  257.                 currentStylesheet = _globalStyleSheet;
  258.                 if (currentStylesheet == null) throw new Error(_errorGlobalStylesheet);
  259.                 styleTarget = getDefaultStyle("."+style);
  260.             }
  261.             else {
  262.                 // specified stylesheet
  263.                 currentStylesheet = _stylesheet;
  264.                 styleTarget = currentStylesheet.getStyle("."+style);
  265.             }
  266.             applyTextFieldValues(styleTarget);
  267.             var styleTextFormat:TextFormat = currentStylesheet.transform(styleTarget);
  268.             defaultTextFormat = styleTextFormat;
  269.             _style = style;
  270.         }
  271.        
  272.         // PUBLIC
  273.         //________________________________________________________________________________________________
  274.        
  275.         /**
  276.          * set a style from a stylesheet (no dot), example: somatext.setStyle("myStyle")
  277.          * @param style style name from a stylesheet
  278.          * @param stylesheet stylesheet of the style. If none is passed, the global stylesheet must be set and will use (SomaText.globalStyleSheet)
  279.          */
  280.         public function setStyle(style:String, stylesheet:StyleSheet = null):void {
  281.             if (stylesheet != null) _stylesheet = stylesheet; // if stylesheet passed, set to private
  282.             if (styleSheet != null) styleSheet = null; // is TextField.styleSheet exists, set to null to apply TextFormat
  283.             applyStyle(style);
  284.             if (_stylesheet != null) styleSheet = _stylesheet; // reassign stylesheet
  285.         }
  286.        
  287.         /**
  288.          * get the style applied to the TextField
  289.          * @return String
  290.          */
  291.         public function get style():String {
  292.             return _style;
  293.         }
  294.        
  295.         /**
  296.          * @inheritDoc
  297.          */
  298.         override public function set type(value:String):void {
  299.             var currentText:String = text;
  300.             var currentHtmlText:String = htmlText;
  301.             switch (value) {
  302.                 case TextFieldType.INPUT:
  303.                     styleSheet = null;
  304.                     break;
  305.                 case TextFieldType.DYNAMIC:
  306.                     if (_stylesheet != null) styleSheet = _stylesheet;
  307.                     break;
  308.             }
  309.             super.type = value;
  310.             text = currentText;
  311.             htmlText = currentHtmlText;
  312.         }
  313.        
  314.         /**
  315.          * remove style and stylesheet from a TextField to show the default values and/or set new styles and stylesheet
  316.          */
  317.         public function resetToDefault():void {
  318.             var currentText:String = text;
  319.             var currentHtmlText:String = htmlText;
  320.             _stylesheet = null;
  321.             styleSheet = null;
  322.             for (var i:int=0; i<_defaultProperties.length; i++) {
  323.                 this[_defaultProperties[i]] = _defaultValues[i];
  324.             }
  325.             text = currentText;
  326.             htmlText = currentHtmlText;
  327.         }
  328.        
  329.         /**
  330.          * shortcut to set TextField properties, example: somatext.setProperties({multiline: true, selectable: true});
  331.          * @param properties Object of TextField properties
  332.          */
  333.         public function setProperties(properties:Object):void {
  334.             for (var prop:String in properties) {
  335.                 if (this.hasOwnProperty(prop)) {
  336.                     this[prop] = properties[prop];
  337.                 }
  338.             }
  339.         }
  340.        
  341.         override public function toString():String {
  342.             var str:String = "----- SomaText properties -------------------------------------\n";
  343.             var listProperties:XMLList = describeType(this)..*.(name() == "accessor" && (@access == "readwrite" || @access == "readonly") && (@declaredBy == "flash.text::TextField" || @declaredBy == "flash.text::TextFormat" || @declaredBy == "com.soma.view::SomaText"));
  344.             for (var i:int=0; i<listProperties.length(); i++) {
  345.                 str += listProperties[i].@name + ": " + this[listProperties[i].@name] + "\n";
  346.             }
  347.             str += "----- SomaText style properties (defaultTextFormat) -----------\n";
  348.             var listPropertiesTFD:XMLList = describeType(defaultTextFormat)..*.(name() == "accessor" && (@access == "readwrite" || @access == "readonly") && (@declaredBy == "flash.text::TextFormat"));
  349.             for (var j:int=0; j<listPropertiesTFD.length(); j++) {
  350.                 str += listPropertiesTFD[j].@name + ": " + defaultTextFormat[listPropertiesTFD[j].@name] + "\n";
  351.             }
  352.             return str;
  353.         }
  354.        
  355.         /**
  356.          * set the global stylesheet, this stylesheet will be used each time you create a new instance (SomaText.globalStyleSheet = myGlobalStylesheetLoaded)
  357.          * @param value StyleSheet
  358.          */
  359.         public static function get globalStyleSheet():StyleSheet {
  360.             return _globalStyleSheet;
  361.         }
  362.        
  363.         /** @private */
  364.         public static function set globalStyleSheet(value:StyleSheet):void {
  365.             _globalStyleSheet = value;
  366.         }
  367.        
  368.         override public function set multiline(value:Boolean):void {
  369.             // set multiline after set an HTML text seems to remove the formatting
  370.             // this function solve the problem
  371.             var currentText:String = text;
  372.             var currentHtmlText:String = htmlText;
  373.             super.multiline = value;
  374.             text = currentText;
  375.             htmlText = currentHtmlText;
  376.         }
  377.        
  378.     }
  379. }

Vote in HexoSearch
2 Responses to “SomaText”
  1. Martin says:

    Hi there,

    The line in which you use this:
    obj[prop] *= .01;

    - what is that for really? Is that for fixing a bug - and is that bug still in the Flash Player 10?

  2. Romuald says:

    Hi Martin,

    No that's not for a bug fixing. It is because you can't say in the stylesheet "alpha: 0.5" and parse it correctly, the value in the stylesheet is based to a range 0-100, and this line put back the value to a range 0-1 (50 * 0.01 = 0.5), the proper range Flash is waiting for.

    For example if you want a "target.alpha = 0.5", you'll write in the stylesheet "alpha: 50". This applies to alpha, scaleX and scaleY.

    Romu

Leave a Reply