4 Replies Latest reply: Feb 19, 2013 9:32 AM by SethKarma RSS

    Calculate width of formatted text in spark label?

    Stenrap2 Community Member

      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?

        • 1. Re: Calculate width of formatted text in spark label?
          Stenrap2 Community Member

          Nobody?

           

          ActionScript has very ****** support for determing the width/height of formatted text in a Flex component. I've wasted way too much time on this!

          • 2. Re: Calculate width of formatted text in spark label?
            Adobe Joe Kenobi Community Member

            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; 

                    }

            • 3. Re: Calculate width of formatted text in spark label?
              DonMitchinson Community Member

              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?

              • 4. Re: Calculate width of formatted text in spark label?
                SethKarma Community Member

                This problem cost me some time!

                 

                Try

                 

                     _label.invalidateSize();

                 

                And eventually

                 

                     _label.validateNow();

                 

                Before calling

                 

                     _label.getExplicitOrMeasuredWidth();