I had to write this for work and then we ended up taking care of the code a different way. Didn't want to lose it. This util converts Strings (variable names) from camelCase to under_score, since the underscore way was how the php backend preferred it. I know the formatting is messed up; just copy and paste it; it has to do with the sizes of the tabs. Enjoy!
package utils
{
/**
* This class for converting variables from camelCase to underscore_vars or vice versa.
*/
public class VariableConverter
{
//-----------------------------------------------------------------------------------------
//
// Variables
//
//-----------------------------------------------------------------------------------------
/**
* The character codes for the letters 'A' and 'Z'.
*/
protected static const A:uint = 65;
protected static const Z:uint = 90;
/**
* If 'camelToUnder' == true, convert the variable name from camelCase to under_score
* format, else vice versa.
*/
public static function convert(value:String, camelToUnder:Boolean = true):String
{
// The var 'camelToUnder', when true, specifies that this function
//-- treat incoming vars as camelCase for converting to underscore format. If set to
//-- false, incoming vars are converted from underscore format to camelCase.
for (var i:uint = 0; i < value.length; i++)
{
// Find the index of each significant character: a capital letter if
//-- 'camelToUnder' == true, and an underscore if false. Splice what happened
//-- before the character to the altered or deleted character and what
//-- should come after it.
var str:String = value.charAt(i);
if (camelToUnder && str.charCodeAt(0) >= A && str.charCodeAt(0) <= Z)
{
var beginning:String = value.substring(0, i);
var ending:String = value.substring(i + 1, value.length);
value = beginning + '_' + str.toLowerCase() + ending;
// Add to 'i' because the String is longer, now.
i++;
}
else if (!camelToUnder && str == '_')
{
beginning = value.substr(0, i);
ending = value.substr(i + 2, value.length);
value = beginning + value.charAt(i + 1).toUpperCase() + ending;
// Subtract from 'i' because the string is shorter, now.
i--;
}
}
return value;
}
} // End class
}
Wednesday, October 29, 2008
Tuesday, May 13, 2008
Re-sizeable (Resizeable) Text Area for Flex 3 without scrollbar
Flex's TextArea class always uses a scrollbar to compensate for text too large to fit, but I wanted to find a way to resize without it. All of the examples that I found online that worked only took into account the height, so here's what I came up with to make the TextArea resize to a nice width to height ratio of 1.5 to 1:
*******************************************************************************
*******************************************************************************
IMPORTANT NOTE!! -- Blogger.com wouldn't let me post the content below without corrupting my CDATA tag. YOU'LL NEED TO ADD THE "!" back in at the appropriate place in the beginning CDATA tag (after the "<") for this example to work!
*******************************************************************************
*******************************************************************************
<[CDATA[
import mx.events.FlexEvent;
[Bindable]
private var myText:String;
[Bindable]
private var starSpangled:String = 'Oh, say, can you see, by the dawn\'s early light what so proudly we ' +
'hailed at the twilight\'s last gleaming?' +
'Whose broad stripes and bright stars, through the perilous fight, o\'er the ramparts we watched,' +
'were so galantly streaming?' +
'And the rockets\' red glare, the bombs bursting in air, gave proof through the night that our' +
'flag was still there. Oh, say, does that star-spangled banner yet wave o\'er the land of the free' +
'and the home of the brave?';
private function onCC():void
{
textArea.addEventListener(FlexEvent.UPDATE_COMPLETE, resizeTA);
myText = starSpangled;
}
private function resizeTA(e:FlexEvent):void
{
// Calculate the minimum area required.
var width:Number = textArea.width;
var height:Number = textArea.textHeight + 22;
var minRequiredArea:Number = width * height;
// Calculate how to get that with the desired ratio of width:height = 1.5:1
var ratio:Number = width/height;
while (!(ratio >= 1.5)) {
width += 1;
height -= 1;
ratio = width/height;
}
// Set the width and height to the new values.
textArea.explicitWidth = width;
textArea.explicitHeight = height;
textArea.removeEventListener(FlexEvent.UPDATE_COMPLETE, resizeTA);
}
]]>
*******************************************************************************
*******************************************************************************
IMPORTANT NOTE!! -- Blogger.com wouldn't let me post the content below without corrupting my CDATA tag. YOU'LL NEED TO ADD THE "!" back in at the appropriate place in the beginning CDATA tag (after the "<") for this example to work!
*******************************************************************************
*******************************************************************************
<[CDATA[
import mx.events.FlexEvent;
[Bindable]
private var myText:String;
[Bindable]
private var starSpangled:String = 'Oh, say, can you see, by the dawn\'s early light what so proudly we ' +
'hailed at the twilight\'s last gleaming?' +
'Whose broad stripes and bright stars, through the perilous fight, o\'er the ramparts we watched,' +
'were so galantly streaming?' +
'And the rockets\' red glare, the bombs bursting in air, gave proof through the night that our' +
'flag was still there. Oh, say, does that star-spangled banner yet wave o\'er the land of the free' +
'and the home of the brave?';
private function onCC():void
{
textArea.addEventListener(FlexEvent.UPDATE_COMPLETE, resizeTA);
myText = starSpangled;
}
private function resizeTA(e:FlexEvent):void
{
// Calculate the minimum area required.
var width:Number = textArea.width;
var height:Number = textArea.textHeight + 22;
var minRequiredArea:Number = width * height;
// Calculate how to get that with the desired ratio of width:height = 1.5:1
var ratio:Number = width/height;
while (!(ratio >= 1.5)) {
width += 1;
height -= 1;
ratio = width/height;
}
// Set the width and height to the new values.
textArea.explicitWidth = width;
textArea.explicitHeight = height;
textArea.removeEventListener(FlexEvent.UPDATE_COMPLETE, resizeTA);
}
]]>
Labels:
change size,
Flex 3,
re-sizeable,
resizeable,
TextArea
Subscribe to:
Posts (Atom)