1

我正在创建这样的标签组件

var label:Label = new Label();
label.text = "some text";
label.styleName = "someStyle";
addChild(label);

但在我明确设置宽度和高度之前,它一直是不可见的。
如何使标签根据其文本自动调整大小?

4

3 回答 3

4

我在这里
找到了我的问题的答案 解决方案是为标签调用 measureText() 函数

var lineMetrics:TextLineMetrics = label.measureText(label.text);
label.width = lineMetrics.width;
label.height = lineMetrics.height;
于 2011-03-12T13:48:29.310 回答
3

我还注意到上述答案似乎不适用于火花组件。然而,这对我有用。

label.width = label.measuredWidth; label.height = label.measuredHeight;

于 2011-09-09T01:49:52.990 回答
1

您应该能够使用label.percentWidth = 100;允许标签随文本自动增长。如果您希望它保持在单行上,您还需要设置该maxDisplayedLines = 1;属性。

您可能还想使用addElement(label)而不是addChild(label).

于 2011-03-11T21:33:04.427 回答