Flex 4 Auto-Resizeable TextArea Component

package cgjc.component {
	import flash.events.Event;

	import mx.controls.Alert;
	import mx.controls.Text;

	public class AdjustHeightText extends Text {
		public function AdjustHeightText() {
			super();
			this.addEventListener(Event.CHANGE, adjustHeightHandler);
		}

		private function adjustHeightHandler(event:Event):void {
			Alert.show("textField.getLineMetrics(0).height: " + textField.getLineMetrics(0).height + "\ntextHeight:" + textField.textHeight);
			height = textField.textHeight;
			//super.verticalScrollPolicy = "on";
			if (height <= textField.textHeight + textField.getLineMetrics(0).height) {
				height = textField.textHeight;
				validateNow();
			}
		}

		//该方法是Text空间自适应文本高度的方法
		/*
		override protected function measure():void {
			super.measure();
			//Alert.show("textField.getLineMetrics(0).height: " + textField.getLineMetrics(0).height + "\ntextHeight:" + textField.textHeight);
			measuredHeight = measuredMinHeight = textField.getExplicitOrMeasuredHeight();
		}
		*/

		override public function set text(val:String):void {
			textField.text = val;
			validateNow();
			height = textField.textHeight;
			validateNow();
		}

		override public function set htmlText(val:String):void {
			textField.htmlText = val;
			validateNow();
			height = textField.textHeight;
			validateNow();
		}

		override public function set height(value:Number):void {
			if (textField == null) {
				if (height <= value) {
					super.height = value;
				}
			} else {
				var currentHeight:uint = textField.textHeight + textField.getLineMetrics(0).height;
				if (currentHeight <= super.maxHeight) {
					if (textField.textHeight != textField.getLineMetrics(0).height) {
						super.height = currentHeight;
					}
				} else {
					super.height = super.maxHeight;
				}
			}
		}
	}
}
原文链接: http://www.eonflex.com/?p=398
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值