複数行のTextArea Flexコンポーネントの場合、テキストの入力を継続し、TextAreaを垂直方向に自動サイズ変更して、入力したすべてのテキストを一度に表示できるようにします。しかし、TextAreaは、レイアウトフロー内の任意のコンポーネントをプッシュダウンする必要があります。その代わりに、TextAreaを上に重ねて配置します。テキスト入力が完了すると、TextAreaは元に戻って通常の範囲に再描画されます。Flex TextAreaの自動サイズ変更動作は可能ですか?
2
A
答えて
0
TextAreaが入っているコンテナがキャンバスのような '絶対的な位置付け'を使用している場合、これは機能します。 TextAreaのtextHeightを測定し、TextAreaの高さ内の特定の範囲に達すると、高さを大きくします。しかし、TextAreaがの背後にあるの部分を引き伸ばすことができますので、あなたはまだzオーダーを修正する必要があります。
0
TextAreaクラスをサブクラス化し、測定された寸法をテキスト領域のテキストのサイズに設定するmeasure()メソッドをオーバーライドします。イベントリスナーを追加して、サブクラス化されたTextAreaのサイズと親サイズを、テキスト入力または親の再レイアウトで無効にすることもできます。
これは、私が作成した単純なクラスです:
public class AutoAdjustTextArea extends TextArea{
/////////////////////////////////////////////////
//Constructor Method/////////////////////////////
/////////////////////////////////////////////////
public function AutoAdjustTextArea():void{
super.addEventListener(FlexEvent.ADD, this.invalidateSizeOnEvent, false, 0, true);
super.addEventListener(Event.CHANGE, this.invalidateSizeOnEvent, false, 0, true);
super.addEventListener(TextEvent.TEXT_INPUT, this.invalidateSizeOnEvent, false, 0, true);
super.addEventListener(ResizeEvent.RESIZE, this.invalidateSizeOnEvent, false, 0, true);
}
/////////////////////////////////////////////////
//Set Methods////////////////////////////////////
/////////////////////////////////////////////////
override public function set text(value:String):void{
super.text = value;
this.invalidateSizeOnEvent();
}
/////////////////////////////////////////////////
//Measure Methods////////////////////////////////
/////////////////////////////////////////////////
override protected function measure():void{
//Calls the super method
super.measure();
//Calls to ensure this is validated
super.validateNow();
super.textField.validateNow();
//Grabs the min and max height values
var minHeight:Number = super.minHeight;
var maxHeight:Number = super.maxHeight;
//Grabs the height of the text
var textHeight:Number = super.textField.textHeight + 4;//+4 for the two pixel gutter on the top and bottom
//Calculates the preferredHeight
var preferredHeight:Number = textHeight;
if(isNaN(minHeight) == false && preferredHeight < minHeight)
preferredHeight = minHeight;
else if(isNaN(maxHeight) == false && preferredHeight > maxHeight)
preferredHeight = maxHeight;
//Sets the measured dimensions
super.measuredHeight = preferredHeight;
}
/////////////////////////////////////////////////
//Event Listener Methods/////////////////////////
/////////////////////////////////////////////////
private function invalidateSizeOnEvent(event:Event = null):void{
super.invalidateProperties();
super.invalidateSize();
super.invalidateParentSizeAndDisplayList();
}