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
Actionscript:
-
package com.soundstep.utils {
-
-
import flash.text.StyleSheet;
-
import flash.text.TextField;
-
import flash.text.TextFieldType;
-
import flash.text.TextFormat;
-
import flash.utils.describeType;
-
-
/**
-
* <b>Author:</b> Romuald Quantin - <a href="http://www.soundstep.com/" target="_blank">www.soundstep.com</a><br />
-
* <b>Class version:</b> 1.0.1<br />
-
* <b>Actionscript version:</b> 3.0<br />
-
* <b>Copyright:</b>
-
* Mozilla Public License 1.1 (MPL 1.1)<br />
-
* <a href="http://www.opensource.org/licenses/mozilla1.1.php" target="_blank">http://www.opensource.org/licenses/mozilla1.1.php</a><br />
-
* <b>Date:</b> 01-2009<br />
-
* <b>Usage:</b>
-
* SomaText is extending the TextField class and add a powerful Stylesheet Management with parsable properties.<br />
-
* 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 />
-
* 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>.
-
* @example
-
* <listing version="3.0">
-
* SomaText.globalStyleSheet = _globalStylesheetLoaded;
-
* // text
-
* var s1:SomaText = new SomaText("simple text");
-
* addChild(s1);
-
* // text with style from the global stylesheet
-
* var s2:SomaText = new SomaText("text with style from the global stylesheet", "simpleStyle");
-
* addChild(s2);
-
* // text with style from another stylesheet
-
* var s3:SomaText = new SomaText("text with style from another stylesheet", "simpleStyle", _customStylesheet);
-
* addChild(s3);
-
* // multiline text with properties set in actionscript
-
* var s4:SomaText = new SomaText("multiline text with textfield properties modified in the actionscript: width, multiline, wordWrap, selectable, and so on.", "defaultText");
-
* s4.width = 220;
-
* s4.multiline = true;
-
* s4.wordWrap = true;
-
* s4.selectable = true;
-
* // can be set in the constructor or using the setProperties method:
-
* //s4.setProperties({width: 220, multiline: true, wordWrap: true, selectable: true});
-
* addChild(s4);
-
* // multiline text with properties set in the stylesheet
-
* var s5:SomaText = new SomaText("multiline text with textfield properties set in the stylesheet: width, multiline, word-wrap, selectable, and so on.", "defaultTextMultiline");
-
* addChild(s5);
-
* // multiline html text
-
* var p1:String = '<p>paragraph 1, <b>multiline html text</b> with <i>more style</i> in the <span class="color1">same SomaText</span> instance.</p>';
-
* var p2:String = '<p class="para">paragraph 2, <b>multiline html text</b> with <i>more style</i> in the <span class="color1">same SomaText</span> instance.</p>';
-
* var s6:SomaText = new SomaText(p1 + p2, "defaultTextMultiline");
-
* addChild(s6);
-
* // text input
-
* var s7:SomaText = new SomaText("text input", "defaultTextMultiline");
-
* s7.type = TextFieldType.INPUT;
-
* s7.autoSize = TextFieldAutoSize.NONE;
-
* s7.width = 220;
-
* s7.height = 20;
-
* s7.background = true;
-
* s7.backgroundColor = 0xFFFFFF;
-
* addChild(s7);
-
* // text with link
-
* var s8:SomaText = new SomaText('text with <a href="http://www.soundstep.com/somaprotest/www/#/stylesheet/" target="_blank">link and hover</a>', "defaultText");
-
* addChild(s8);
-
* </listing>
-
* @inheritDoc
-
*/
-
-
public class SomaText extends TextField {
-
-
//------------------------------------
-
// private, protected properties
-
//------------------------------------
-
-
// global stylesheet used as a default
-
private static var _globalStyleSheet:StyleSheet;
-
// current style
-
private var _style:String;
-
// current stylesheet
-
private var _stylesheet:StyleSheet;
-
// store the defaultTextFormat of the TextField
-
private var _defaultTextFormat:TextFormat;
-
// store the default properties of the TextField
-
private var _defaultProperties:Array;
-
// store the default values of the TextField
-
private var _defaultValues:Array;
-
// error message if the global stylesheet is missing
-
private var _errorGlobalStylesheet:String = "Error in SomaText, the global Stylesheet must be specified using SomaText.globalStyleSheet = myGlobalStyleSheet";
-
-
//------------------------------------
-
// public properties
-
//------------------------------------
-
-
/**
-
* default type that will be used for each new instance created (TextFieldType)
-
*/
-
public static var DEFAULT_TYPE:String = "dynamic";
-
/**
-
* default font embedded value that will be used for each new instance created
-
*/
-
public static var DEFAULT_EMBED_FONT:Boolean = false;
-
/**
-
* default alias that will be used for each new instance created (AntiAliasType)
-
*/
-
public static var DEFAULT_ANTIALIAS:String = "advanced";
-
/**
-
* default autosize that will be used for each new instance created (TextFieldAutoSize)
-
*/
-
public static var DEFAULT_AUTOSIZE:String = "left";
-
/**
-
* default multiline value that will be used for each new instance created
-
*/
-
public static var DEFAULT_MULTILINE:Boolean = false;
-
/**
-
* default wordWrap value that will be used for each new instance created
-
*/
-
public static var DEFAULT_WORDWRAP:Boolean = false;
-
/**
-
* default selectable value that will be used for each new instance created
-
*/
-
public static var DEFAULT_SELECTABLE:Boolean = false;
-
/**
-
* default condenseWhite value that will be used for each new instance created
-
*/
-
public static var DEFAULT_CONDENSE_WHITE:Boolean = true;
-
/**
-
* default mouseWheelEnabled value that will be used for each new instance created
-
*/
-
public static var DEFAULT_MOUSEWHEEL_ENABLED:Boolean = false;
-
/**
-
* default doubleClickEnabled value that will be used for each new instance created
-
*/
-
public static var DEFAULT_DOUBLECLICK_ENABLED:Boolean = false;
-
/**
-
* default gridFitType value that will be used for each new instance created (GridFitType)
-
*/
-
public static var DEFAULT_GRIDFITTYPE:String = "none";
-
/**
-
* default useRichTextClipboard value that will be used for each new instance created
-
*/
-
public static var DEFAULT_RICHTEXT_CLIPBOARD_ENABLED:Boolean = true;
-
-
//------------------------------------
-
// constructor
-
//------------------------------------
-
-
/**
-
* create a new instance of the SomaText class
-
* @param text or htmlText value
-
* @param style of a stylesheet (without dot), you must register a global stylesheet (SomaText.globalStyleSheet) or pass a stylesheet in the third parameter
-
* @param object of properties, shortcut to set properties to the TextField, example: {multiline: true, selectable: true}
-
*/
-
public function SomaText(value:String = "", style:String = null, stylesheet:StyleSheet = null, properties:Object = null) {
-
super();
-
init(value, style, stylesheet, properties);
-
}
-
-
//
-
// PRIVATE, PROTECTED
-
//________________________________________________________________________________________________
-
-
/** @private */
-
// change a string to a Boolean (when styles are parsed)
-
private function stringToBoolean(value:String):Boolean {
-
return (value.toLowerCase() == "true" || value.toLowerCase() == "1");
-
}
-
-
/** @private */
-
// change a string to a Boolean (when styles are parsed)
-
private function getDefaultStyle(style:String):Object {
-
var ds:StyleSheet = _globalStyleSheet;
-
if (ds == null) throw new Error(_errorGlobalStylesheet);
-
return ds.getStyle(style);
-
}
-
-
/** @private */
-
// initialize the TextField
-
protected function init(value:String = "", style:String = null, stylesheet:StyleSheet = null, properties:Object = null):void {
-
setDefault();
-
memorizeProperties();
-
_stylesheet = stylesheet;
-
// apply style
-
if (style != null) applyStyle(style);
-
// apply stylesheet
-
if (style != null || stylesheet != null) {
-
var currentStylesheet:StyleSheet = (_stylesheet != null) ? _stylesheet: _globalStyleSheet;
-
_stylesheet = currentStylesheet;
-
styleSheet = _stylesheet;
-
}
-
if (properties != null) setProperties(properties);
-
if (value.indexOf("<") != -1 && value.indexOf(">") != -1) {
-
htmlText = value;
-
}
-
else {
-
text = value;
-
}
-
}
-
-
/** @private */
-
// memorize the default properties to reset the style of the TextField
-
protected function memorizeProperties():void {
-
_defaultTextFormat = defaultTextFormat;
-
_defaultProperties = [];
-
_defaultValues = [];
-
var listProperties:XMLList = describeType(this)..*.(name() == "accessor" && @access == "readwrite" && @declaredBy == "flash.text::TextField");
-
for (var i:int=0; i<listProperties.length(); i++) {
-
_defaultProperties.push(listProperties[i].@name);
-
_defaultValues.push(this[listProperties[i].@name]);
-
}
-
}
-
-
/** @private */
-
// set default properties from global values
-
protected function setDefault():void {
-
type = SomaText.DEFAULT_TYPE;
-
embedFonts = SomaText.DEFAULT_EMBED_FONT;
-
antiAliasType = SomaText.DEFAULT_ANTIALIAS;
-
autoSize = SomaText.DEFAULT_AUTOSIZE;
-
multiline = SomaText.DEFAULT_MULTILINE;
-
wordWrap = SomaText.DEFAULT_WORDWRAP;
-
selectable = SomaText.DEFAULT_SELECTABLE;
-
condenseWhite = SomaText.DEFAULT_CONDENSE_WHITE;
-
mouseWheelEnabled = SomaText.DEFAULT_MOUSEWHEEL_ENABLED;
-
doubleClickEnabled = SomaText.DEFAULT_DOUBLECLICK_ENABLED;
-
gridFitType = SomaText.DEFAULT_GRIDFITTYPE;
-
useRichTextClipboard = SomaText.DEFAULT_RICHTEXT_CLIPBOARD_ENABLED;
-
}
-
-
/** @private */
-
// parse an object and apply the properties to the TextField
-
protected function applyTextFieldValues(obj:Object):void {
-
for (var prop:String in obj) {
-
if (this.hasOwnProperty(prop)) {
-
if (this[prop] is Boolean) {
-
obj[prop] = stringToBoolean(obj[prop]);
-
}
-
else if (prop == "backgroundColor" || prop == "borderColor") {
-
obj[prop] = parseInt(String(obj[prop]).substring(1), 16);
-
}
-
else if (this[prop] is int) {
-
obj[prop] = parseInt(obj[prop]);
-
}
-
// specific actions
-
if (prop == "alpha" || prop == "scaleX" || prop == "scaleY") {
-
obj[prop] *= .01;
-
}
-
// apply
-
this[prop] = obj[prop];
-
}
-
}
-
}
-
-
/** @private */
-
// apply the style to the TextField
-
protected function applyStyle(style:String):void {
-
var currentStylesheet:StyleSheet;
-
var styleTarget:Object;
-
if (_stylesheet == null) {
-
// unspecified stylesheet
-
currentStylesheet = _globalStyleSheet;
-
if (currentStylesheet == null) throw new Error(_errorGlobalStylesheet);
-
styleTarget = getDefaultStyle("."+style);
-
}
-
else {
-
// specified stylesheet
-
currentStylesheet = _stylesheet;
-
styleTarget = currentStylesheet.getStyle("."+style);
-
}
-
applyTextFieldValues(styleTarget);
-
var styleTextFormat:TextFormat = currentStylesheet.transform(styleTarget);
-
defaultTextFormat = styleTextFormat;
-
_style = style;
-
}
-
-
// PUBLIC
-
//________________________________________________________________________________________________
-
-
/**
-
* set a style from a stylesheet (no dot), example: somatext.setStyle("myStyle")
-
* @param style style name from a stylesheet
-
* @param stylesheet stylesheet of the style. If none is passed, the global stylesheet must be set and will use (SomaText.globalStyleSheet)
-
*/
-
public function setStyle(style:String, stylesheet:StyleSheet = null):void {
-
if (stylesheet != null) _stylesheet = stylesheet; // if stylesheet passed, set to private
-
if (styleSheet != null) styleSheet = null; // is TextField.styleSheet exists, set to null to apply TextFormat
-
applyStyle(style);
-
if (_stylesheet != null) styleSheet = _stylesheet; // reassign stylesheet
-
}
-
-
/**
-
* get the style applied to the TextField
-
* @return String
-
*/
-
public function get style():String {
-
return _style;
-
}
-
-
/**
-
* @inheritDoc
-
*/
-
override public function set type(value:String):void {
-
var currentText:String = text;
-
var currentHtmlText:String = htmlText;
-
switch (value) {
-
case TextFieldType.INPUT:
-
styleSheet = null;
-
break;
-
case TextFieldType.DYNAMIC:
-
if (_stylesheet != null) styleSheet = _stylesheet;
-
break;
-
}
-
super.type = value;
-
text = currentText;
-
htmlText = currentHtmlText;
-
}
-
-
/**
-
* remove style and stylesheet from a TextField to show the default values and/or set new styles and stylesheet
-
*/
-
public function resetToDefault():void {
-
var currentText:String = text;
-
var currentHtmlText:String = htmlText;
-
_stylesheet = null;
-
styleSheet = null;
-
for (var i:int=0; i<_defaultProperties.length; i++) {
-
this[_defaultProperties[i]] = _defaultValues[i];
-
}
-
text = currentText;
-
htmlText = currentHtmlText;
-
}
-
-
/**
-
* shortcut to set TextField properties, example: somatext.setProperties({multiline: true, selectable: true});
-
* @param properties Object of TextField properties
-
*/
-
public function setProperties(properties:Object):void {
-
for (var prop:String in properties) {
-
if (this.hasOwnProperty(prop)) {
-
this[prop] = properties[prop];
-
}
-
}
-
}
-
-
override public function toString():String {
-
var str:String = "----- SomaText properties -------------------------------------\n";
-
var listProperties:XMLList = describeType(this)..*.(name() == "accessor" && (@access == "readwrite" || @access == "readonly") && (@declaredBy == "flash.text::TextField" || @declaredBy == "flash.text::TextFormat" || @declaredBy == "com.soma.view::SomaText"));
-
for (var i:int=0; i<listProperties.length(); i++) {
-
str += listProperties[i].@name + ": " + this[listProperties[i].@name] + "\n";
-
}
-
str += "----- SomaText style properties (defaultTextFormat) -----------\n";
-
var listPropertiesTFD:XMLList = describeType(defaultTextFormat)..*.(name() == "accessor" && (@access == "readwrite" || @access == "readonly") && (@declaredBy == "flash.text::TextFormat"));
-
for (var j:int=0; j<listPropertiesTFD.length(); j++) {
-
str += listPropertiesTFD[j].@name + ": " + defaultTextFormat[listPropertiesTFD[j].@name] + "\n";
-
}
-
return str;
-
}
-
-
/**
-
* set the global stylesheet, this stylesheet will be used each time you create a new instance (SomaText.globalStyleSheet = myGlobalStylesheetLoaded)
-
* @param value StyleSheet
-
*/
-
public static function get globalStyleSheet():StyleSheet {
-
return _globalStyleSheet;
-
}
-
-
/** @private */
-
public static function set globalStyleSheet(value:StyleSheet):void {
-
_globalStyleSheet = value;
-
}
-
-
override public function set multiline(value:Boolean):void {
-
// set multiline after set an HTML text seems to remove the formatting
-
// this function solve the problem
-
var currentText:String = text;
-
var currentHtmlText:String = htmlText;
-
super.multiline = value;
-
text = currentText;
-
htmlText = currentHtmlText;
-
}
-
-
}
-
}


Entries (RSS)
January 20th, 2009 at 8:41 am
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?
January 20th, 2009 at 10:44 am
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