代码之家  ›  专栏  ›  技术社区  ›  user56512

在指定文本之前,如何设置动态UITextField的正确宽度?

  •  3
  • user56512  · 技术社区  · 17 年前

    在Flex3.2中,我创建了一个UITextField,然后测量将要分配给该字段的text属性的文本。然后,我使用这些度量设置字段的大小。但是,计算的宽度不够宽,无法容纳文本。实现正确测量的顺序是否不同,或者我是否发现measureText()函数有问题?我怎样才能得到准确的结果?

    // UITextField's default size appears to be 100x100
    // Measure the text then set width and height
    var tf:UITextFormat = uiTextField.getUITextFormat();
    var tlm:TextLineMetrics = tf.measureText(this.labelText);
    
    // Text within the field is clipped unless 'padding' is added to the size
    // Flex Documentation specifies there is a 2 px gutter on each side, so
    // the expected padding would be 4 px.  However, clipping occurs, for 
    // "Hello, World" up to 9 px.
    uiTextField.width = tlm.width + 9; 
    uiTextField.height = tlm.height + 4;
    uiTextField.border = true;
    uiTextField.name = "uiTextField";               
    uiTextField.text = this.labelText;
    
    2 回复  |  直到 17 年前
        1
  •  4
  •   James Hay    17 年前

    我以前在测量文本字段的宽度和高度时遇到过各种各样的麻烦。看起来您只是想自动调整文本字段的大小。您是否尝试过:

    uiTextField.autoSize = TextFieldAutoSize.LEFT;
    

    ???

        2
  •  0
  •   cwallenpoole    17 年前

    不幸的是,Flex在尝试动态获取textWidth时经常会感到非常困惑。更糟糕的是,很难可靠地捕获错误并正确地更新Flex本身。我找到的最佳选择是:

    1. mx.controls.Text hack AS3 描述一种执行此操作的方法。
    2. 使用callLater或FlexEvent.CREATION_完成事件侦听器——这不太可靠,但绝对不是黑客攻击
    推荐文章