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

发布于 2024-12-29 22:58:38 字数 35 浏览 1 评论 0原文

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

A君 2025-01-05 22:58:38

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

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;
}
同尘 2025-01-05 22:58:38

您尝试过以下方法吗?

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

Have you tried the following?

var lineMetrics:TextLineMetrics = label.measureText(label.text);
var result:Number = lineMetrics.height;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文