如何获得Spark标签中文本的行高?

时间:2012-01-31 00:22:46

标签: flex flex4 flex-spark

如何确定Spark Label中文本的行高(以像素为单位)?

2 个答案:

答案 0 :(得分:2)

keyle的答案只会给你文字的高度,而不是实际的'lineHeight'风格。您可以轻松地获得这样的风格:

myLabel.getStyle("lineHeight");

问题是,这可以返回相对值(百分比)或绝对值(以像素为单位)。默认情况下 - 如果没有明确设置lineHeight,则为"120%"

所以这里是我们如何在两种情况下获得像素值:

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;
}

答案 1 :(得分:1)

您是否尝试过以下操作?

var lineMetrics:TextLineMetrics = label.measureText(label.text);
var result:Number = lineMetrics.height;