Not yet rated

Problem

I want a TextArea that will act like a TextInput that increases its width if user types text longer than TextArea width.

Solution

Extend TextArea, override the setters for the text and width properties, and add an event listener for the text change event.

Detailed explanation

---------------------------- DynamicWidthTextArea.as ------------------------------

package {
  import flash.events.Event;
 
  import mx.controls.TextArea;
 
  public class DynamicWidthTextArea extends TextArea{
    public function DynamicWidthTextArea(){
      super();
      super.horizontalScrollPolicy = "off";
      super.verticalScrollPolicy = "off";
      super.wordWrap = false;
      this.addEventListener(Event.CHANGE, adjustWidthHandler);
    }
   
    private function adjustWidthHandler(event:Event):void{
      if(width <= textField.textWidth +
textField.getLineMetrics(0).width){
        width = textField.textWidth;     
        validateNow();
      }
    }
   
    override public function set text(val:String):void{
      textField.text = val;
      validateNow();
      width = textField.textWidth;
      validateNow();
    }
   
    override public function set htmlText(val:String):void{
      textField.htmlText = val;
      validateNow();
      width = textField.textWidth;
      validateNow();
    }
    override public function set width(value:Number):void{
      if(textField == null){
        if(width <= value){
          super.width = value;
        }
      }else{       
        trace("width: " + width);
        trace("super.maxWidth: " + super.maxWidth);
        if (textField.getLineMetrics(0).width<= super.maxWidth){
          trace("textField.textWidth: " + textField.textWidth);
          trace("textField.getLineMetrics(0).width: " +
textField.getLineMetrics(0).width);
          if(width <= textField.getLineMetrics(0).width + 10){
            super.width = textField.getLineMetrics(0).width + 10;
          }        
        }else{
            super.width = super.maxWidth;         
        }  
      }
    }
   
    override public function get text():String{
        return textField.text;
    }
   
    override public function get htmlText():String{
        return textField.htmlText;
    }
  
    override public function set maxWidth(value:Number):void{
      super.maxWidth = value;
    }
  }
}


 

 

---------------------------------- Test.mxml ---------------------------------

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx=" http://www.adobe.com/2006/mxml"
  xmlns:comp="*">
  <mx:HBox>
    <mx:Label text="Font size 8: "/>
    <comp:DynamicWidthTextArea width="100" fontSize="8"/>   
  </mx:HBox>
  <mx:HBox>
    <mx:Label text="Font size 10: "/>
    <comp:DynamicWidthTextArea width="100" fontSize="10"/>   
  </mx:HBox>
  <mx:HBox>
    <mx:Label text="Font size 12: "/>
    <comp:DynamicWidthTextArea width="100" fontSize="12"/>   
  </mx:HBox>
  <mx:HBox>
    <mx:Label text="Font size 20: "/>
    <comp:DynamicWidthTextArea width="100" fontSize="20"/>   
  </mx:HBox>
  <mx:HBox>
    <mx:Label text="Font size 40: "/>
    <comp:DynamicWidthTextArea width="100" height="100"
fontSize="40"/>   
  </mx:HBox>
  <mx:HBox>
    <mx:Label text="Font size 100: "/>
    <comp:DynamicWidthTextArea width="200" height="120"
fontSize="100"/>   
  </mx:HBox>
</mx:Application>


 


+
This work is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License. Permissions beyond the scope of this license, pertaining to the examples of code included within this work are available at Adobe.

Report abuse

Related recipes