getStyle (StyleSheet.getStyle method)

public getStyle(name:String) : Object

Returns a copy of the style object associated with the specified style (name). If there is no style object associated with name, the method returns null.

Availability: ActionScript 1.0; Flash Player 7

Parameters

name:String - The name of the style to retrieve.

Returns

Object - A style object; otherwise null.

Example

The following example loads styles from a CSS file, parses the StyleSheet and displays style names and property values in the Output panel. Create a ActionScript file called StyleSheetTracer.as and enter the following code in the file:

import TextField.StyleSheet;
class StyleSheetTracer {
    // StyleSheetTracer.displayFromURL 
    // This method displays the CSS style sheet at
    // the specified URL in the Output panel.
    static function displayFromURL(url:String):Void {
        // Create a new StyleSheet object
        var my_styleSheet:StyleSheet = new StyleSheet();
        // The load operation is asynchronous, so set up
        // a callback function to display the loaded StyleSheet. 
        my_styleSheet.onLoad = function(success:Boolean) {
            if (success) {
                StyleSheetTracer.display(this);
            } else {
                trace("Error loading style sheet "+url);
            }
        };
        // Start the loading operation.
        my_styleSheet.load(url);
    }
    static function display(my_styleSheet:StyleSheet):Void {
        var styleNames:Array = my_styleSheet.getStyleNames();
        if (!styleNames.length) {
            trace("This is an empty style sheet.");
        } else {
            for (var i = 0; i<styleNames.length; i++) {
                var styleName:String = styleNames[i];
                trace("Style "+styleName+":");
                var styleObject:Object = my_styleSheet.getStyle(styleName);
                for (var propName in styleObject) {
                    var propValue = styleObject[propName];
                    trace("\t"+propName+": "+propValue);
                }
                trace("");
            }
        }
    }
}

Create a CSS document called styles.css, which has two styles called .heading and .mainBody that define properties for font-family, font-size and font-weight. Enter the following code in the CSS document:

 .heading {
 font-family: Arial, Helvetica, sans-serif;
 font-size: 24px;
 font-weight: bold;
 }
 .mainBody {
 font-family: Arial, Helvetica, sans-serif;
 font-size: 12px;
 font-weight: normal;
 }

Finally, in a FLA or ActionScript file, enter the following ActionScript to load the external style sheet, styles.css:

StyleSheetTracer.displayFromURL("styles.css");

This displays the following in the Output panel:

 Style .heading:
 fontWeight: bold
 fontSize: 24px
 fontFamily: Arial, Helvetica, sans-serif

 Style .mainBody:
 fontWeight: normal
 fontSize: 12px
 fontFamily: Arial, Helvetica, sans-serif

See also

setStyle (StyleSheet.setStyle method), getStyleNames (StyleSheet.getStyleNames method)