admin管理员组文章数量:1430093
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.
I use a lot of mon 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;
};
And I'm calling the label like so:
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
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);
they all return 0.
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.
EDIT: After some trial and error, I've found that
qLabel.toImage().height;
returns some number, however, it doesn't seem to be the correct height.
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.
I use a lot of mon 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;
};
And I'm calling the label like so:
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
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);
they all return 0.
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.
EDIT: After some trial and error, I've found that
qLabel.toImage().height;
returns some number, however, it doesn't seem to be the correct height.
Share Improve this question edited Oct 23, 2012 at 6:20 Sarah R asked Oct 22, 2012 at 13:24 Sarah RSarah R 1072 silver badges9 bronze badges 2- Which platform you are working on? iPhone, iPad or Android? – Roozbeh Zabihollahi Commented Oct 22, 2012 at 18:33
- I'm working for iPhone. Edited that into the question. – Sarah R Commented Oct 23, 2012 at 6:20
2 Answers
Reset to default 5You can use postlayout event
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. :)
I've ran into the same problem and I didn't want to use a postlayout event on every single element. Instead I used the size property which is a read only and contains the actual sizes of the element. I pared this with the event data from a postlayout event and they gave the exact same values.
So if you want to get the height of an element I suggest you using $.label1.size.height
instead of adding an event listener all the time. This will also be alot better for the performance of your app because the postlayout event will be fired alot.
本文标签: javascriptHow to get the actual height of a label with autoheightStack Overflow
版权声明:本文标题:javascript - How to get the actual height of a label with auto-height - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745550921a2662939.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论