views:

290

answers:

2

Hi, I am now developing a Flex application in which I need to control each pixel of my control of Flex. I want to calculate that how width and how height of some text used in my control and do some layout stuff.After some searching I find that the only way to draw text in Flex is to use something like TextField. So, I use TextField to display text and try to get the width and height of the text through:

textfiled.getLineMetrics(0).width/.height;

But the real textfield is much more bigger than this, so I do:

textfield.width = textfiled.getLineMetrics(0).width; textfield.height = textfiled.getLineMetrics(0).height;

But, the textfield get part not displayed, and I am surprised by this effect. I know there should be a 2-pixel gutter around the text, but what the remain space? Why nearly 20% part of the text height/width are not displayed?

And how can I get a real drawText call in Flex, I mean something like Windows's drawText method...

Thanks!

+1  A: 

you can try this : getCharBoundaries () i've used it and seems accurate hoever it is for char and not a whole text so you will need to iterate through the text

also see this :

http://stackoverflow.com/questions/1942911/is-there-a-way-to-get-the-actual-bounding-box-of-a-glyph-in-actionscript

Eran
Yeah, I got a lot of valuable info from the link, thanks!
Yang Bo
A: 

Please try:

textfield.width = textfield.textWidth + 4;
textfield.height = textfield.textHeight + 4;

I'm not familiar with Windows's drawText method so I cannot help you there, but perhaps you want to have a look at the stellar text layout functionality in Flex 4.

Wouter VdB
Yes, I have also tried to add some more extra space for the TextField and it works to some extent, thanks!
Yang Bo