在分配文本之前如何设置动态 UITextField 的正确宽度?

发布于 2024-07-14 07:17:59 字数 824 浏览 3 评论 0原文

在 Flex 3.2 中,我创建一个 UITextField,然后测量要分配给该字段的文本属性的文本。 然后,我使用这些指标来设置字段的大小。 但是,计算出的宽度不足以容纳文本。 是否有不同的顺序来实现正确的测量,或者我发现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;

In Flex 3.2, I'm creating a UITextField, then measuring text that I'm about to assign to that field's text property. I then use those metrics to set the size of the field. However, the calculated width is not wide enough to accommodate the text. Is there a different order to achieve proper measurement or am I seeing a problem with the measureText() function? How can I get accurate results?

// 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;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

浮生面具三千个 2024-07-21 07:17:59

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

uiTextField.autoSize = TextFieldAutoSize.LEFT;

???

I've had all sorts of trouble with measuring the width and heights of textFields before. It looks like you just want to autosize the textField. Have you tried:

uiTextField.autoSize = TextFieldAutoSize.LEFT;

???

糖果控 2024-07-21 07:17:59

不幸的是,当 Flex 尝试动态获取 textWidth 时,它常常会感到非常困惑。 更糟糕的是,很难可靠地捕获错误并让 Flex 正确更新自身。 我发现的最好的选择:

  1. 手动破解它——大多数是可靠的,并且具有在一切完成渲染之前发生的好处:
    mx.controls.Text hack AS3 描述了一种执行此操作的方法。
  2. 使用 callLater 或 FlexEvent.CREATION_COMPLETE 事件监听器——这不太可靠,但它绝对不是黑客
  3. 使用 setTimeout 的延迟小于 1/10 秒(我喜欢 25-50 毫秒)——我发现这是最可靠的,但它可能会导致屏幕上出现一个小“斑点”(通常短期内不会非常明显)代码)。

Unfortunately, Flex often gets very confused when it tries to dynamically get a textWidth. Even worse, it is difficult to reliably catch the error and have Flex update itself correctly. The best options I've found:

  1. Hack it manually -- mostly reliable and has the benefit of happening before everything has finished rendering:
    mx.controls.Text hack AS3 describes a way to do this.
  2. Use callLater or an FlexEvent.CREATION_COMPLETE event listener -- this is less reliable, but it is definitely not a hack
  3. Use setTimeout with a delay of less than 1/10 a second ( I like 25-50 milliseconds ) -- I have found this the most reliable, but it may cause a small "blip" on the screen (generally not terribly noticeable for short code).
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文