User Guide

Table Of Contents
Writing the component’s ActionScript code 57
By default, Flex sets the values of _measuredPreferredWidth and _measuredPreferredHeight
to the values of the current height and width, but you should override them. The following
example of the
measure() method from the Label component sets a default width and height if
the label text field is empty:
function measure(Void):Void {
var myTF = _getTextFormat();
var txt = text;
if (txt == undefined || txt.length < 2) {
txt = "Wj";
}
var textExt = myTF.getTextExtent2(txt);
var textW = textExt.width + 4;
var textH = textExt.height + 4;
if (textW == undefined) {
textW = 20;
}
if (textH == undefined) {
textH = 8;
}
trace("Label: " + textW + " " + textH);
_measuredPreferredWidth = textW;
_measuredPreferredHeight = textH;
}
Implementing the layoutChildren() method
The
layoutChildren() method positions subobjects within the confines set by the
layoutWidth and layoutHeight properties of your component. Each component should
implement this method. The
layoutChildren() method deprecates the size() method, which
is used primarily by Flash designers to perform the same function.
Use the width and height properties of the child controls when changing the size of the children.
These properties are scaled for use inside the component. Use the
layoutWidth and
layoutHeight properties when changing the size of the component itself. These properties are
not scaled because the component is at the top level.
The
layoutChildren() method does not update the screen unless you call an invalidation
method. Flex only calls the
layoutChildren() method if the invalidateLayout() method was
previously called. For more information, see About invalidation” on page 68.
The following example checks for the value of the
labelPlacement property and lays out the
mode_mc object accordingly:
function layoutChildren():Void {
text_mc.setSize(layoutWidth - mode_mc.width, layoutHeight);
if (labelPlacement == "left")