Custom Label Components
Any given Flex application will probably use many labels to display information. The only downside is that you often want the value (test) assigned to the label to be formatted a certain way without having to parse your input each time, assign it to your a label's text property, then style the label as needed. All of this can be done by extending the label itself which gives you a customized label component that can be re-used throughout your application either as a simple label or as an ItemRenderer within a DataGrid.
For Example:
<?xml version="1.0" encoding="utf-8"?>
<mx:Label xmlns:mx="http://www.adobe.com/2006/mxml" creationComplete="init()">
<mx:Script>
<![CDATA[
private const POSITIVE_COLOR:uint = 0x000000; // Black
private const NEGATIVE_COLOR:uint = 0xCC0000; // Red
private function init():void {
if(this.listData != null) {
this.text = data[this.listData["dataField"]];
}
}
override public function set text(value:String):void {
if(value.match(/^-?d+.?d*$/)) {
var amount:Number = Number(value);
var str:String = "$";
if(Math.abs(amount) < 1000) {
str += amount;
} else if(Math.abs(amount) < 1000000) {
str += int((amount / 1000)*100) / 100;
str += "k";
} else if(Math.abs(amount) < 1000000000) {
str += int((amount / 1000000)*100) / 100;
str += "m";
} else {
str += int((amount / 1000000000)*10) / 10;
str += "bn";
}
super.text = str;
setStyle("color", (amount < 0) ? NEGATIVE_COLOR : POSITIVE_COLOR);
} else {
super.text = value;
}
}
]]>
</mx:Script>
</mx:Label>


Loading... Please Wait