Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/actionscript-3/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/scala/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Actionscript 3 如何设置多行文字环绕文本字段,以便在设置高度时自动调整其宽度?_Actionscript 3_Flash - Fatal编程技术网

Actionscript 3 如何设置多行文字环绕文本字段,以便在设置高度时自动调整其宽度?

Actionscript 3 如何设置多行文字环绕文本字段,以便在设置高度时自动调整其宽度?,actionscript-3,flash,Actionscript 3,Flash,我想创建一个textfield扩展,它: 设置宽度后,将根据文本内容自动调整高度大小。通过自动向左调整大小、换行为真、多行为真,操作简单。 设置高度后,将根据文本内容自动调整宽度大小这是我的问题。 当宽度和高度设置都不是我感兴趣的情况时 我在互联网上尝试了几件事,但都被难倒了。这不是最优雅的解决方案,但它应该能起作用: function setHeight(newHeight:Number):void { myTextField.height = newHeight; while(my

我想创建一个textfield扩展,它: 设置宽度后,将根据文本内容自动调整高度大小。通过自动向左调整大小、换行为真、多行为真,操作简单。 设置高度后,将根据文本内容自动调整宽度大小这是我的问题。

当宽度和高度设置都不是我感兴趣的情况时


我在互联网上尝试了几件事,但都被难倒了。

这不是最优雅的解决方案,但它应该能起作用:

function setHeight(newHeight:Number):void {
  myTextField.height = newHeight;

  while(myTextField.textHeight > myTextField.height) {
    myTextField.width += 100;
  }
}

一般的解决方案是不可能的,因为如果文本字段包含太多的换行符,无法在给定的高度内显示,那么无论指定什么宽度,文本字段都无法显示所有的行。部分解决方案是通过greetification来表示的,但它缺少一些应该注意的特性。首先,无论您做什么,都不应将“高度”设置为小于字体高度的值,否则文本字段将无法显示一行。其次,如果
wordWrap
设置为false,并且
multiline
设置为true,则生成的
textWidth
是文本字段的最大理想宽度,因此如果您按照greetification建议调整宽度,则在达到记录的
textWidth
后停止,因为进一步增加是没有意义的

function setHeight(newHeight:Number):void {
  var tw:Number;
  var th:Number;
  if (myTextField.wordwrap) {
    myTextField.wordwrap=false;
    tw=myTextField.textWidth;
    th=myTextField.textHeight;
    myTextField.wordwrap=true;
  } else {
    tw=myTextField.textWidth;
    th=myTextField.textHeight;
  }
  if (newHeight<th) newHeight=th+2; // as below
  myTextField.height = newHeight;

  while((myTextField.textHeight > myTextField.height)&&(myTextField.width<tw)) {
    myTextField.width += 100;
  }
  if (myTextField.width>tw) myTextField.width=tw+2; // "2" depends on text format
  // and other properties, so either play with it or assume a number big enough
}
函数setHeight(newHeight:Number):无效{
var-tw:数字;
var-th:数字;
if(myTextField.wordwrap){
myTextField.wordwrap=false;
tw=myTextField.textWidth;
th=myTextField.textHeight;
myTextField.wordwrap=true;
}否则{
tw=myTextField.textWidth;
th=myTextField.textHeight;
}
if(newHeight myTextField.height)&&(myTextField.widthtw)myTextField.width=tw+2;/“2”取决于文本格式
//和其他属性,所以要么使用它,要么假设一个足够大的数字
}

这只是我的头顶(未测试),但请尝试将要调整大小的设置为
NaN
(不是数字)。许多自动GUI元素只有在禁用手动对应项时才启用,
NaN
是数字的
null
版本。同样,这是未经测试的,但它是供您测试的一行代码。如果这样做有效,我建议扩展
TextField
并覆盖高度/宽度设置器,以便自动为您执行此操作。