Skip to content

Instantly share code, notes, and snippets.

@skovalyov
Created June 5, 2012 08:12
Show Gist options
  • Save skovalyov/2873541 to your computer and use it in GitHub Desktop.
Save skovalyov/2873541 to your computer and use it in GitHub Desktop.
Left aligned label in FormItem
package {
import flash.display.DisplayObject;
import mx.containers.FormItem;
import mx.controls.Label;
import mx.styles.CSSStyleDeclaration;
import mx.styles.StyleManager;
[Style(name="labelAlign", type="String", enumeration="left,right", inherit="no")]
public class ExtendedFormItem extends FormItem {
private static const CLASS_NAME : String = "ExtendedFormItem";
private static var classConstructed : Boolean = staticConstructor();
private static function staticConstructor() : Boolean {
if (!StyleManager.getStyleDeclaration(CLASS_NAME)) {
var newStyleDeclaration : CSSStyleDeclaration = new CSSStyleDeclaration();
newStyleDeclaration.setStyle("labelAlign", "left");
StyleManager.setStyleDeclaration(CLASS_NAME, newStyleDeclaration, true);
}
return true;
}
override public function styleChanged(styleProp : String) : void {
super.styleChanged(styleProp);
switch (styleProp) {
case "labelAlign" :
invalidateDisplayList();
break;
}
}
override protected function updateDisplayList(w : Number, h : Number) : void {
super.updateDisplayList(w, h);
if (label.length > 0) {
var labelAlign : String = getStyle("labelAlign");
var labelIndex : int = (required ? rawChildren.numChildren - 2 : rawChildren.numChildren - 1);
var label : Label = Label(rawChildren.getChildAt(labelIndex));
if (labelAlign == "left") {
label.x = 0;
if (required) {
var indicator : DisplayObject = rawChildren.getChildAt(rawChildren.numChildren - 1);
indicator.x = label.width + ((getStyle("indicatorGap") - indicator.width) / 2);
}
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment