Skip navigation
Currently Being Moderated

Calculate width of formatted text in spark label?

Mar 3, 2011 12:08 PM

Hi all,


I'm instantiating a spark label in an ActionScript class, then setting various styles on it (such as fontFamily and fontSize):


var versionLabel:Label = new Label();
versionLabel.text = "10.1.1 SP1";
versionLabel.setStyle("fontFamily", '_verdana');
versionLabel.setStyle("fontSize", 26);


After doing all this, I'm tracing the width of the text in the label:


trace("The 'measured' width of versionLabel is:" + versionLabel.measureText(versionLabel.text).width);


However, this always gives me the width of the text as if I had not set any styles. In the case above, the on-screen width is 290 pixels with the formatting and 127 without, but the trace always returns 127.


What am I doing wrong here? Does this have something to do with the fact that I'm using an embedded font? How can I get the width of the formatted text?

  • Currently Being Moderated
    Jan 12, 2012 1:45 PM   in reply to Stenrap2

    So I know this is real late, but hopefully if someone else has this problem and runs accross this they might find my solution helpful. This one way I used to solve this issue. I created the following function:


    private function getTextWidth(target:Label):Number


                var txtField:TextField = new TextField();

                var txtFormat:TextFormat = new TextFormat();

                txtFormat.font = target.getStyle("fontFamily");

                txtFormat.size = target.getStyle("fontSize");

                txtField.defaultTextFormat = txtFormat;

                txtField.multiline = true;

                txtField.wordWrap = true;

                txtField.width = target.width;

                txtField.text = label.text;

                return txtField.textWidth; 


    Mark as:
  • Currently Being Moderated
    Jan 12, 2012 4:42 PM   in reply to Adobe Joe Kenobi

    This looks like a good solution on paper, I'll have to try it out.

    I managed to hack a temp solution for extending number of datagrid table rows

    based on content in the data cell, but it was very kludgey and only worked for my specific font.


    Will this return textHeight as well?

    Mark as:
  • Currently Being Moderated
    Feb 19, 2013 9:32 AM   in reply to Stenrap2

    This problem cost me some time!






    And eventually




    Before calling



    Mark as:

More Like This

  • Retrieving data ...

Bookmarked By (0)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points