Styles in Flex can define things like colors, fonts, and other visual characteristics, which makes styling one of the most powerful mechanisms in Flex, but also one of the most complex mechanisms. Mastering styling is key to creating expressive Flex and AIR applications.

This chapter is from the book

This chapter is from the book

Every visual component in the Flex framework has a default appearance, including colors, shape, font, and more. Although this default look, called the Halo theme, looks nice as a default, a custom look can help your application stand out among other Flex applications. The Flex framework allows you to customize the look of components using special properties called styles. Styles can define things like colors, fonts, and other visual characteristics, which makes styling one of the most powerful mechanisms in Flex, but also one of the most complex mechanisms. Mastering styling is key to creating expressive Flex and AIR applications.

Using styling you can easily change the appearance of your application. Each Flex component has its own set of style properties that allow you to alter different parts of it to create a custom look. If you're looking for the quickest way to divert from the default Halo Flex theme without too much effort, styling is the way to get there. Figure 4-1 shows variations of a button made to look different using just style properties, starting with the default look.

Understanding Style Properties

Not all properties of visual components can be specified as styles. Typically, style properties are those that modify only the appearance of a visual component such as color-related properties, font and font size, padding, and so on. Table 4-1 shows the types of properties that can be classified as styles and those that cannot. Style properties are used to specify skins and other visual elements (see Chapter 5, "Graphical Skinning," for more about skinning).

Table 4-1. Flex Properties That Can Be Applied as Styles and Those That Cannot

With styling you can specify:

Fonts

Text formatting

Colors

Icons, indicators and cursors

Skins

Alignment

Relative positioning

Padding

With styling you cannot specify:

Absolute positioning

Size

Event handlers

Effects and transitions

States

Filters

Component properties

Inheritance

Components may inherit styles from their parent containers. Consider a Button component within a Canvas container where the Canvas has the color style set to 0xFF0000 (red) as shown in Listing 4-1. Even though color has not been set directly on the Button component, it will still have a red label, just by virtue of being a child of the Canvas (see Figure 4-2).

Listing 4-1. The value of the color style that is set on the Canvas component will cascade to the Button component within

Inheritance is a very powerful function of the styling mechanism, as it allows top-level style changes to propagate throughout the application. This behavior, coupled with the various CSS selectors discussed in the upcoming "Applying Styles with CSS" section, make it possible to change the visuals of an entire application rather easily.

NOTE

Not all style properties are inheritable. Refer to the Adobe Flex 3 Language Reference for more information.

Data Types and Formats

With the exception of class references and embedded assets, styles can only be of type String, Number, or an Array of Strings and Numbers. To validate values, styles have a format associated with them. For example, the backgroundColor style of a Canvas is associated with the "color" format, which means it can be specified as either hexadecimal, RGB, or qualified color name.

Table 4-2 describes the relationship between various data types, formats, and the units/syntax available when specifying styles.

Table 4-2. Property Formats

Name

Sample styles

Units / Syntax

Length (Number)

fontSize

pixels (px)

style-name: length [unit]

borderThickness

inches (in)

horizontalGap

centimeters (cm)

paddingTop

millimeters (mm)

paddingBottom

points (pt)

paddingLeft

picas (pc)

paddingRight

keywords, for fontSize only:

xx-small

x-small

small

medium

large

x-large

xx-large

Time (Number)

openDuration

milliseconds

style-name: time

closeDuration

selectionDuration

Color (String or Number)

backgroundColor

hexadecimal (#000000 - #FFFFF)

style-name: color

borderColor

RGB (r%, g%, b%)

color

VGA color names:

textSelectedColor

Aqua

Black

Blue

Fuschia

Gray

Green

Lime

Maroon

Navy

Olive

Purple

Red

Silver

Teal

White

Yellow

NOTE

Embedding assets and using class references are discussed at length in Chapter 5 and Chapter 9.