Calculating Text Width In ActionScript And Flex

前端 未结 8 2139
离开以前
离开以前 2020-12-30 00:47

I\'m trying to calculate how WIDE to make my button, based on the text that it will contain, and when I try to google for how to calcuate something as simplistic as the WIDT

相关标签:
8条回答
  • 2020-12-30 01:16

    Here's a way that works also:

    var tempText:Text = new Text();
    tempText.regenerateStyleCache(false);
    var textWidth:int = tempText.measureText(*yourstring*).width;
    
    0 讨论(0)
  • 2020-12-30 01:19

    Joshua, it really helps to be clear. Are you talking TextField, MX Label, Spark Label, RichText, etc? Different text components use different text engines, such as FTE and TLF and may have different solutions. I certainly wish Adobe had a good set of utilities or sample code which could predict what the size of font rendered onto the controls would be, before you actually do it. But, the good news is that in certain cases - like, a good old fashioned TextField, you can predict this pretty well. You just make a TextField, set it's textFormat field, auto size method and the text. You should be able to get it's size before adding it anywhere. I don't remember what the order was, but, I remember the order you set those properties matters. If you can't figure out how to do it, I can provide a code example. Now, for the new, "improved", components such as Spark Labels - I'll be buggered if I can find a damn way... spent a number of hours on this and haven't found a way.. or someone who knows a way :P.

    0 讨论(0)
提交回复
热议问题