更改文本的文本并在Flex中获取measuredWidth

时间:2021-02-19 04:57:27

I have a Text element in mxml. The text value is changing by user actions. I want to set the width to the exact size of the text. Since the width property is the actual component's width, I need the measuredWidth property after the FlexEvent.UPDATE_COMPLETE event is triggered:

我在mxml中有一个Text元素。文本值正在由用户操作更改。我想将宽度设置为文本的确切大小。由于width属性是实际组件的宽度,因此在触发FlexEvent.UPDATE_COMPLETE事件后我需要measuredWidth属性:

textField.addEventListener(FlexEvent.UPDATE_COMPLETE, function(e:FlexEvent):void{
    textField.width = textfield.measuredWidth;
});

This only changes to the proper width the first time. After that it only can be smaller but not larger. I've read the Text and the UIComponent source, and it said I need to call invalidateSize(). But it only works if I set the explicitWidth property NaN. So I made a setter function:

这仅在第一次更改为适当的宽度。之后它只能更小但不会更大。我已经阅读了Text和UIComponent源代码,它说我需要调用invalidateSize()。但它只有在我设置explicitWidth属性NaN时才有效。所以我做了一个setter函数:

function setText(newValue:String):void {
  textField.text = newValue;
  textField.explicitWidth = NaN;
  textField.invalidateSize();
}

But it's still only shrinking. My question is, how can I get always the proper measuredWidth value after the UPDATE_COMPLETE event?

但它仍然只是在缩小。我的问题是,如何在UPDATE_COMPLETE事件之后始终获得正确的measuredWidth值?

Thanks

谢谢

2 个解决方案

#1


1  

Changing the Text element to Label solves the problem.

将Text元素更改为Label可解决问题。

#2


0  

I agree with some of the commenters that there may be better alternatives that achieve the same result. However, you could use the measureText function of your Text element to determine the necessary width:

我同意一些评论者的观点,认为可能有更好的替代品可以达到相同的效果。但是,您可以使用Text元素的measureText函数来确定必要的宽度:

#1


1  

Changing the Text element to Label solves the problem.

将Text元素更改为Label可解决问题。

#2


0  

I agree with some of the commenters that there may be better alternatives that achieve the same result. However, you could use the measureText function of your Text element to determine the necessary width:

我同意一些评论者的观点,认为可能有更好的替代品可以达到相同的效果。但是,您可以使用Text元素的measureText函数来确定必要的宽度: