본문 바로가기

Flex/__Flex 3.0

한글수에 따라서 다이나믹하게 늘어나는 MultilineText


<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" xmlns:comp="comp.*"  >
 <mx:Script>
  <![CDATA[
   [Bindable] private var num       : int = 0;
   [Bindable] private var totalNum  : int = 0;
   public function getType1ByteString( str:String ):uint {
    var strlength:uint = str.length;
    var byte:uint = 0;
    for(var i:uint = 0; i< strlength;i++) {
     byte += (escape(str.charAt(i)).length > 4) ? 2 : 1;
    }     
    totalNum = byte;    
    if(byte > 720){
     num = 0;     
     num = ((totalNum - 720) / 120);     
     MT.height = ( 96 ) + ( 16 * (num + 1) );
    }else{
     MT.height = 96;
    }
    return byte;
   }
  ]]>
 </mx:Script>
 
 <mx:Grid width="100%" y="60">
  <mx:GridRow width="100%" height="100%">
   <mx:GridItem width="100%" height="100%" horizontalAlign="center" verticalAlign="middle">
    <comp:MultilineText x="93" y="57" fontSize="11" id="MT" width="666" height="96" change="getType1ByteString(MT.text)"/>
   </mx:GridItem>
  </mx:GridRow> 
  <mx:GridRow width="100%" height="100%">
   <mx:GridItem width="100%" height="100%" horizontalAlign="center" verticalAlign="middle">
    <comp:MultilineText x="93" y="57" fontSize="11" id="MT2" width="666" height="96" change="getType1ByteString(MT.text)"/>
   </mx:GridItem>
  </mx:GridRow>       
 </mx:Grid>  
 
</mx:Application>


MultilineText.as
package comp
{
 import flash.events.FocusEvent;
 import mx.controls.TextArea;
 
 public class MultilineText extends TextArea {
  public function MultilineText(){
   super();   
   addEventListener(FocusEvent.FOCUS_IN, onFocusInHandler);
   addEventListener(FocusEvent.FOCUS_OUT, onFocusOutHandler);
  }
 
  private function onFocusInHandler(event:FocusEvent):void{
   textField.alwaysShowSelection = true;
  }
 
  private function onFocusOutHandler(event:FocusEvent):void{
   textField.alwaysShowSelection = false;
  }
 }
}