Changing Text's text and getting measuredWidth in Flex
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:
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:
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?
Thanks
Changing the Text element to Label solves the problem.
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:
精彩评论