Adobe FLEX 2 - CREATING AND EXTENDING COMPONENTS Manual page 165

Creating and extending flex 2 components
Hide thumbs Also See for FLEX 2 - CREATING AND EXTENDING COMPONENTS:
Table of Contents

Advertisement

In the following example, you override the
it examines the text passed to the control, and calculates the default size of the TextArea
control to display the entire text string in a single line.
package myComponents
{
// asAdvanced/myComponents/MyTextArea.as
import mx.controls.TextArea;
import flash.text.TextLineMetrics;
public class MyTextArea extends TextArea
{
public function MyTextArea() {
super();
}
// The default size is the size of the text plus a 10 pixel margin.
override protected function measure():void {
super.measure();
// Calculate the default size of the control based on the
// contents of the TextArea.text property.
var lineMetrics:TextLineMetrics = measureText(text);
// Add a 10 pixel border area around the text.
measuredWidth = measuredMinWidth = lineMetrics.width + 10;
measuredHeight = measuredMinHeight = lineMetrics.height + 10;
}
}
}
For text strings that are longer than the display area of your application, you can add logic to
increase the height of the TextArea control to display the text on multiple lines. The following
application uses this component:
<?xml version="1.0"?>
<!-- asAdvanced/MainMyTextArea.mxml -->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
xmlns:MyComp="myComponents.*" >
<MyComp:MyTextArea id="myTA" text="This is a long text strring that
would normally cause a TextArea control to display scroll bars. But, the
custom MyTextArea control calcualtes its default size based on the text
size."/>
<mx:TextArea id="flexTA" text="This is a long text strring that would
normally cause a TextArea control to display scroll bars. But, the custom
MyTextArea control calcualtes its default size based on the text size."/>
</mx:Application>
method of the TextArea control so that
measure()
Implementing the component
165

Advertisement

Table of Contents
loading

Table of Contents