如何使用自动高度获取标签的实际高度 [英] How to get the actual height of a label with auto-height

查看:168
本文介绍了如何使用自动高度获取标签的实际高度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这个问题以前曾以各种形式出现过,但是没有一个解决方案对我有帮助... 我正在使用Titanium API 2.1.3,并正在为iPhone进行构建.

I'm aware that this question has appeared in various forms before, but none of the solutions worked out for me... I'm using the Titanium API 2.1.3, and building for iPhone.

我使用了很多普通的JS,所以我有这个:

I use a lot of common JS, so I have this:

exports.Header = function(title){
var l = Ti.UI.createLabel({
    text: title,
    height: 'auto',
    textAlign: 'left',
    color: '#989898',
    font: {fontSize: exports.defaultFontSize+10, fontFamily: exports.defaultFont, fontWeight: 'bold'}
});
return l;
};

我这样称呼标签:

var qLabel = gui.Header(question);
qLabel.top = 5;
qLabel.left = 10;
qLabel.right = 10;
qLabel.color = "#3B3B3B";
qLabel.font = {fontSize: gui.defaultFontSize+4, fontWeight: 'bold'};

例如,我已经尝试了各种方法来获得标签的高度,

I've tried all sorts of things to get the height of the label so far, for instance:

qLabel.toImage().height // this returns auto
qLabel.getRect().height // this returns 0

和getSize().height(也返回0)或getHeight()(返回自动)相同.我尝试过先将其添加到父视图中,然后再读取高度,但是什么也没做...我做的最后一件事是:

and the same thing with getSize().height (this returns 0 as well) or getHeight() (returns auto). I've tried adding it to the parent view first, and then reading the height, but nothing... the last thing I did was this:

var qqv = Ti.UI.createView({ width: 'auto', height: 'auto' });

qqv.add(qLabel);
qLabel.show();

Ti.API.info(qLabel.rect.height);
Ti.API.info(qLabel.size.height);
Ti.API.info(qqv.rect.height);
Ti.API.info(qqv.size.height);

它们都返回0.

这时我感到非常绝望,我不知道这个问题是否是由于我使用的API版本或其他原因而引起的.

I'm getting pretty desperate at this point, and I don't know if this problem is because of the API version I'm using or whatever...any help is appreciated.

经过一番尝试和错误后,我发现了

After some trial and error, I've found that

qLabel.toImage().height;

返回 some 数字,但是,它似乎不是正确的高度.

returns some number, however, it doesn't seem to be the correct height.

推荐答案

您可以使用这是获取标签高度的示例代码

here is the sample code that will get your label height

label.addEventListener('postlayout', function(e) {
    var label_height = e.source.rect.height;
    alert(label_height);
});

希望有帮助. :)

hope that helped. :)

这篇关于如何使用自动高度获取标签的实际高度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆