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