setStyle (StyleSheet.setStyle method)

public setStyle(name:String, style:Object) : Void

Adds a new style with the specified name to the StyleSheet object. If the named style does not already exist in the StyleSheet, it is added. If the named style already exists in the StyleSheet, it is replaced. If the style parameter is null, the named style is removed.

Flash Player creates a copy of the style object that you pass to this method.

For a list of supported styles, see the table in the description for the StyleSheet class.

Availability: ActionScript 1.0; Flash Player 7

Parameters

name:String - The name of the style to add to the StyleSheet.

style:Object - An object that describes the style, or null.

Example

The following example adds a style named emphasized to the StyleSheet myStyleSheet. The style includes two style properties: color and fontWeight. The style object is defined with the {} operator.

 myStyleSheet.setStyle("emphasized", {color:'#000000',fontWeight:'bold'});

You could also create a style object using an instance of the Object class, and then pass that object (styleObj) as the style parameter, as the next example shows:

import TextField.StyleSheet;
var my_styleSheet:StyleSheet = new StyleSheet();

var styleObj:Object = new Object();
styleObj.color = "#000000";
styleObj.fontWeight = "bold";
my_styleSheet.setStyle("emphasized", styleObj);
delete styleObj;

var styleNames_array:Array = my_styleSheet.getStyleNames();
for (var i=0;i<styleNames_array.length;i++) {
    var styleName:String = styleNames_array[i];
    var thisStyle:Object = my_styleSheet.getStyle(styleName);
    trace(styleName);
    for (var prop in thisStyle) {
        trace("\t"+prop+": "+thisStyle[prop]);
    }
    trace("");
}

The following information appears in the Output panel:

 emphasized
 fontWeight: bold
 color: #000000

Note: Because Flash Player creates a copy of the style object you pass to setStyle(), the delete styleObj command in the code example reduces memory usage by deleting the original style object passed to setStyle().

See also

{} object initializer operator, StyleSheet (TextField.StyleSheet)