2

複数行の TextArea Flex コンポーネントの場合、テキストを入力し続け、TextArea を垂直方向に自動サイズ変更して、入力したすべてのテキストが一度に表示されるようにします。ただし、TextArea は、レイアウト フローでコンポーネントを押し下げたいと考えています。代わりに、TextArea をそれらの上に拡張する必要があります。テキストの入力が完了すると、TextArea は元に戻り、通常の境界に再描画されます。

4

2 に答える 2

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();
    }
于 2013-08-25T05:59:08.533 に答える
0

TextArea が入っているコンテナが (Canvas のように) 「絶対」配置を使用している場合、これは機能します。TextArea で textHeight を測定するだけで、TextArea の高さ内の特定の範囲に達したら、高さを大きくします。ただし、TextArea が他のコンポーネントの背後に引き伸ばされる可能性があるため、z オーダーを修正する必要があります。

于 2009-08-03T14:12:00.013 に答える