7 Replies Latest reply on Jan 19, 2011 1:40 AM by RUSH-ME

    How to get the Label's height (Spark) after setting its fontSize?

    jKnistrum

      Hi,

       

      in Flex 4, inside a class which extends Spark.Group I add a Label

       

      // x axes's label

      xTitle = new Label();

      xTitle.text = this.titleX;

      xTitle.setStyle("fontSize", 72 ) ;

       

      it works fine, but

       

      after several attempts with FontMetrics and  label.getExplicitOrMeasuredHeight(), label.getBounds() and so on,  I don't get to know how to retrieve the Label's height, it always return zero.

       

      Thanks any help,

       

      jcarlos

        • 1. Re: How to get the Label's height (Spark) after setting its fontSize?
          Wrataxas Level 2

          In one of Flex's skins I saw it use getPreferredBoundsWidth() to determine how wide a Label thought it was, so perhaps getPreferredBoundsHeight() will give you what you need.

          1 person found this helpful
          • 2. Re: How to get the Label's height (Spark) after setting its fontSize?
            jKnistrum Level 1

            It shows a number related to Label rotation but it doesn't take the fontSize and Face in account.

             

            I've made some simple tests

             

            <?xml version="1.0" encoding="utf-8"?>

            <s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"

              xmlns:s="library://ns.adobe.com/flex/spark"

              xmlns:mx="library://ns.adobe.com/flex/mx" creationComplete="init();">

              <s:layout>

                <s:VerticalLayout horizontalAlign="center" paddingTop="20" gap="20"/>

              </s:layout>

              <fx:Script>

                <![CDATA[

            private function init():void

            {

            var nt:testClass = new testClass(debug);

            addElement( nt ) ;

            debug.text += "\n\nMain mxml " + nt.label1.getPreferredBoundsHeight(true) + "\n";

            }

                ]]>

              </fx:Script>

            <s:VGroup>

            <s:TextArea id="debug" width="300" height="200">

            </s:TextArea>

            </s:VGroup>

            </s:Application>

             

            .......

             

            package

            {

            import spark.components.Group;

            import spark.components.Label;

            import spark.components.TextArea;

             

            public class testClass extends Group

            {

            [Embed(source='../fonts/GOUDOS.ttf',

                    fontName='Goudy',

                    mimeType='application/x-font')]

            private var font1:Class;

             

            public var label1:Label;

             

            public function testClass(debug:TextArea)

            {

            super();

            label1 = new Label();

             

            label1.text = "Meu texto" ;

             

            label1.x = 0;

            label1.y = 0;

            label1.width = 200;

             

            label1.setStyle("fontSize", 72 ) ;

            label1.setStyle("fontFamily", "Goudy" ) ;

            label1.rotation = 15;

             

            addElement(label1);

             

            debug.text += "\ntestClass\ngetPreferredBoundsHeight() " + label1.getPreferredBoundsHeight();

            debug.text += "\ngetBounds(this) " + label1.getBounds(this);

            debug.text += "\nheight " + label1.height;

            debug.text += "\ngetLayoutBoundsHeight() " + label1.getLayoutBoundsHeight();

            debug.text += "\ngetMaxBoundsHeight() " + label1.getMaxBoundsHeight();

            debug.text += "\ngetExplicitOrMeasuredHeight() " + label1.getExplicitOrMeasuredHeight();

            }

            }

            }

             

            without success.

             

            testClass

            getPreferredBoundsHeight() 51.763916015625

            getBounds(this) (x=4745318.4, y=8219136, w=0, h=0)

            height 0

            getLayoutBoundsHeight() 51.763916015625

            getMaxBoundsHeight() 12247.467041015625

            getExplicitOrMeasuredHeight() 0

             

            Main mxml 51.763916015625

             

            If the rotation is changed getPreferredBoundsHeight() changes too, but it is not affected by fontSize and fontFamily.

             

            It seems that Sparks have complicated what I want to measure with the mx.TextField where tField.textWidth  adn tField.textHeight give simply and directly what is needed.

            • 3. Re: How to get the Label's height (Spark) after setting its fontSize?
              RUSH-ME Level 3

              Hi

               

              check with labelID.measureText(yourText).height.

               

              Hope this helps

              Rush-me

              • 4. Re: How to get the Label's height (Spark) after setting its fontSize?
                jKnistrum Level 1

                Unfortunately not,

                 

                no matter the fontSize it returns 17

                 

                super();

                label1 = new Label();

                 

                label1.text = "My Text" ;

                label1.width = 300;

                 

                label1.setStyle("fontSize", 12 ) ;

                label1.setStyle("fontFamily", "Goudy" ) ;

                label1.rotation = 15;

                 

                addElement(label1);

                 

                debug.text += "\nlabel1.measureText(label1.text).height " + label1.measureText(label1.text).height;

                label1.setStyle("fontSize", 72 ) ;

                debug.text += "\nlabel1.measureText(label1.text).height " + label1.measureText(label1.text).height;

                 

                 

                 

                jcarlos

                • 5. Re: How to get the Label's height (Spark) after setting its fontSize?
                  Flex harUI Adobe Employee

                  Call validateNow after setStyle

                  • 6. Re: How to get the Label's height (Spark) after setting its fontSize?
                    jKnistrum Level 1

                    Nope.

                     

                    label1.validadeNow() and with this.validateNow()

                     

                     

                    package

                    {

                    import spark.components.Group;

                    import spark.components.Label;

                    import spark.components.TextArea;

                     

                    public class testClass extends Group

                    {

                    [Embed(source='../fonts/GOUDOS.ttf',

                            fontName='Goudy',

                            mimeType='application/x-font')]

                    private var font1:Class;

                     

                    public var label1:Label;

                     

                    public function testClass(debug:TextArea)

                    {

                    super();

                    label1 = new Label();

                     

                    label1.text = "My Text" ;

                    label1.width = 300;

                     

                    label1.setStyle("fontSize", 12 ) ;

                    label1.setStyle("fontFamily", "Goudy" ) ;

                    label1.validateNow();

                    label1.rotation = 15;

                    addElement(label1);

                     

                    debug.text += "\nlabel1.measureText(label1.text).height " + label1.measureText(label1.text).height;

                     

                    label1.setStyle("fontSize", 72 ) ;

                    label1.validateNow();

                     

                    debug.text += "\nlabel1.measureText(label1.text).height " + label1.measureText(label1.text).height;

                    }

                    }

                    }

                    • 7. Re: How to get the Label's height (Spark) after setting its fontSize?
                      RUSH-ME Level 3

                      Hi

                       

                      I am not sure why it is not working in your case. below code is working fine as it shows 17 and 83.

                       

                      <?xml version="1.0" encoding="utf-8"?>
                      <s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
                                     xmlns:s="library://ns.adobe.com/flex/spark"
                                     xmlns:mx="library://ns.adobe.com/flex/mx"
                                     creationComplete="application1_creationCompleteHandler(event)">
                         
                          <fx:Script>
                              <![CDATA[
                                  import mx.events.FlexEvent;
                                 
                                  import spark.components.Label;
                                 
                                  protected function application1_creationCompleteHandler(event:FlexEvent):void
                                  {
                                      var label1:Label = new Label();
                                     
                                      label1.text = "My Text" ;
                                      label1.width = 300;
                                      label1.setStyle("fontSize", 12 ) ;
                                      label1.rotation = 15;
                                     
                                      this.addElement(label1);
                                     
                                      debug.text += "\nlabel1.measureText(label1.text).height " + label1.measureText(label1.text).height;
                                     
                                      label1.setStyle("fontSize", 72 ) ;
                                     
                                      debug.text += "\nlabel1.measureText(label1.text).height " + label1.measureText(label1.text).height;
                                  }

                       

                              ]]>
                          </fx:Script>
                          <s:layout>
                              <s:VerticalLayout/>
                          </s:layout>
                         
                          <s:TextArea id="debug"/>
                      </s:Application>

                       

                      Hope this helps

                      Rush-me