【问题标题】:How do I set up my multiline wordWrap TextField to automatically resize its width when height is set?如何设置我的多行 wordWrap TextField 以在设置高度时自动调整其宽度?
【发布时间】:2013-07-31 21:57:18
【问题描述】:

我想创建一个文本字段扩展: 设置宽度时,根据文本内容自动调整高度。通过 autosize left、word wrap true、multiline true 轻松完成。 设置高度时,根据文本内容自动调整宽度。 这是我的问题。

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

我在网上尝试了几件事,我很难过。

【问题讨论】:

  • 这只是我的想法(未经测试),但尝试将您要调整大小的那个设置为NaN(不是数字)。许多自动 GUI 元素仅在禁用手动对应项且 NaN 是数字的 null 版本时启用。同样,这是未经测试的,但它是供您测试的单行代码。如果可行,我建议扩展 TextField 并覆盖高度/宽度设置器以自动为您完成。

标签: actionscript-3 flash


【解决方案1】:

一般的解决方案是不可能的,好像文本字段包含太多换行符而无法在给定高度内显示,无论您分配什么宽度,文本字段都将无法显示所有行。 Greetification 提供了部分解决方案,但它缺少一些应该注意的功能。首先,无论你做什么,你都不应该将高度设置为小于字体高度的值,否则文本字段将无法显示单行。其次,如果wordWrap 设置为false,multiline 设置为true,则生成的textWidth 是您的文本字段的最大理想宽度,因此如果您按照greetification 的建议调整宽度,一旦达到记录的@ 就停止987654324@,因为进一步增加是没有意义的。

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
}

【讨论】:

    【解决方案2】:

    不是最优雅的解决方案,但它应该可以工作:

    function setHeight(newHeight:Number):void {
      myTextField.height = newHeight;
    
      while(myTextField.textHeight > myTextField.height) {
        myTextField.width += 100;
      }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-06-16
      • 2021-10-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-08-12
      • 2017-11-25
      相关资源
      最近更新 更多