4 Replies Latest reply on Jul 24, 2011 11:11 AM by Francisc

    Get line height of multiline StyleableTextField

    Francisc Level 3

      Hello,

       

      So? How can I do that: get the height in pixels between StyleableTextField lines?

       

      The reason I need this is that if I try to center it vertically, (totalHeight-StyleableTextField.textHeight)/2 it won't consider the vertical spacing and the centering is missing a few pixels.

       

      Thank you.

        • 1. Re: Get line height of multiline StyleableTextField
          Shongrunden Adobe Employee

          I would avoid using the textHeight property.  Does it work better if you use the getElementPreferredHeight() method that LabelItemRenderer provides?  See the renderer I created in this thread for an example.

          • 2. Re: Get line height of multiline StyleableTextField
            Francisc Level 3

            getElementPreferredHeight() does fix the problem.

            You're saying I should use getElementPreferredHeight() and getElementPreferredWidth() instead of other properties like textHeight or simply height in mobile item renderers?

             

            I did read the MultilineLabelItemRenderer you created. It was a good read, taught me a lot. The reason I used textHeight in this particular case was that text was never going to be more than 2 lines, so I figured the added complexity isn't needed for such a simple case.

             

            Thank you.

            • 3. Re: Get line height of multiline StyleableTextField
              Shongrunden Adobe Employee

              Retrieving size information of an element in a renderer via getElementPreferredWidth()/getElementPreferredHeight() is the recommended approach.

               

              getElementPreferredWidth() is a convenience method which works similar to how setElementSize() works in that it calls different APIs depending on what the element is typed as:

               

                  /**

                   *  A helper method to retrieve the preferred width of a child element.

                   *

                   *  <p>This method is the recommended way to get a child element's preferred

                   *  width. You can use this method instead of checking for and using

                   *  various interfaces/classes such as ILayoutElement, IFlexDisplayObject,

                   *  or StyleableTextField.</p>

                   *

                   *  @param element The child element to retrieve the width for. The element could 

                   *  be an ILayoutElement, IFlexDisplayObject, StyleableTextField, or a generic

                   *  DisplayObject.

                   *

                   *  @return The child element's preferred width.

                   *

                   *  @see #getElementPreferredHeight 

                   *

                   *  @langversion 3.0

                   *  @playerversion Flash 10.1

                   *  @playerversion AIR 2.5

                   *  @productversion Flex 4.5

                   */

                  protected function getElementPreferredWidth(element:Object):Number

                  {

                      if (element is ILayoutElement)

                      {

                          return ILayoutElement(element).getPreferredBoundsWidth();

                      }

                      else if (element is IFlexDisplayObject)

                      {

                          return IFlexDisplayObject(element).measuredWidth;

                      }

                      else

                      {

                          return element.width;

                      }

                  }

               

              Measuring a StyleableTextField can get really tricky and you really won't want to do it yourself if you don't need to.  Using getElementPreferredWidth() will call StyleableTextField.getPreferredBoundsWidth() which in turn calls the mx_internal measuredTextSize() method.  Check out the source code of those methods if you are interested in what measurement calculations are done behind the scenes.

              1 person found this helpful
              • 4. Re: Get line height of multiline StyleableTextField
                Francisc Level 3

                OK, Steve. Thank you very much.