对于多行 TextArea Flex 组件,希望能够继续输入文本并让 TextArea 在垂直方向上自动调整大小,以便一次显示所有输入的文本。但是,TextArea 想要在布局流中下推任何组件。相反,希望 TextArea 在它们之上延伸。文本输入完成后,TextArea 应收缩并重新绘制到其正常边界。
问问题
3118 次
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 所在的容器使用“绝对”定位(如画布),这将起作用。只需测量 TextArea 上的 textHeight ,当它到达 TextArea 高度内的某个范围时,将高度变大。但是,您仍然必须修复 z 顺序,因为 TextArea 可能希望向下延伸到其他组件后面。
于 2009-08-03T14:12:00.013 回答