Search code examples
apache-flexflex4flex-spark

How do you get the line height of the text in a Spark Label?


How do you determine the line height (in pixels) of the text in a Spark Label?


Solution

  • keyle's answer will only give you the height of the text, not the actual 'lineHeight' style. You can easily get that style like this:

    myLabel.getStyle("lineHeight");
    

    The problem is that this can return a relative value (a percentage) or an absolute value (in pixels). The default - if no lineHeight was explicitely set - is "120%".

    So here's how we can get the value in pixels in both cases:

    var lineHeightStyle:* = myLabel.getStyle("lineHeight");
    
    //its already a value in pixels
    if (lineHeightStyle is Number) var lineHeight:Number = lineHeightStyle;
    //it's a relative value: let's calculate
    else {
        var lineMetrics:TextLineMetrics = myLabel.measureText(myLabel.text);
        //get the numeric value from the string and divide it by 100
        var ratio:Number = int(lineHeightStyle.match(/\d+/)[0]) / 100;
        lineHeight = lineMetrics.height * ratio;
    }