(i confirms these examples by iOS simulator)
when I use Titanium SDK 1.8.3, i can get a height of Label by the following code.
var label = Ti.UI.createLabel({text: 'fooooo barrrrrr buzzzzzzzz', width: 110, height: 'auto', font: {fontSize: 20}}); win1.add(label); Ti.API.info(label.height); // 72, OK Ti.API.info(label.toImage().height); // 72, OKbut, after 2, i can't get it. i tried the following code on 2.0.2 GA, 2.1.0 GA, 2.1.1 GA and it didn't work.
var label = Ti.UI.createLabel({text: 'fooooo barrrrrr buzzzzzzzz', width: 110, height: 'auto', font: {fontSize: 20}}); win1.add(label); Ti.API.info(label.height); // 'auto' Ti.API.info(label.toImage().height); // 24 ... it maybe a height of only one line label.addEventListener('postlayout', function(e) { // not called ... Ti.API.info(e.source.rect.height); });how can i do that? thanks in advance.
4 Answers
Accepted Answer
Hi Takumi
This code will create a new row, with a title and an optional subtitle, both can be whatever length of text you like and they will automatically size themselves as required within the row - which will also automatically resize itself to fit the contents.
You would need to tweak it to suit your visual needs, but functionally it does everything you want without ANY need to calculate ANY dimensions manually.
Fully tested on iOS and Android, plus I have made it as a function that you can call as option as you like in a loop.
function addRow(obj) { Ti.API.info('title', obj.title); Ti.API.info('subtitle', obj.subtitle); var row = Ti.UI.createTableViewRow({ height: Ti.UI.SIZE, backgroundColor: '#fff', width: Ti.UI.FILL }); var viewParent = Ti.UI.createView({ bottom: 10, // use as margin from the row edges height: Ti.UI.SIZE, layout: 'vertical', left: 10, // use as margin from the row edges right: 10, // use as margin from the row edges top: 10, // use as margin from the row edges width: Ti.UI.FILL }); row.add(viewParent); var lblTitle = Ti.UI.createLabel({ font: { fontSize: 20 }, height: Ti.UI.SIZE, left: 0, text: (obj.title || ''), width: Ti.UI.SIZE }); viewParent.add(lblTitle); if (obj.subtitle) { var lblSubTitle = Ti.UI.createLabel({ color: '#666', // used for contrast only font: { fontSize: 20 }, height: Ti.UI.SIZE, left: 0, text: (obj.subtitle || ''), top: 10, // this will place 10 pixels between the title and the subtitle - adjust to suit your needs width: Ti.UI.SIZE }); viewParent.add(lblSubTitle); } return row; } var data = []; data.push(addRow({ title: 'Title 1', subtitle: 'Sub-title 1' })); data.push(addRow({ title: 'Title 2' })); data.push(addRow({ title: 'Title 3', subtitle: 'Sub-title 3 is really long and with any luck will jump across more than one line within the row.' })); var tbl = Ti.UI.createTableView({ height: Ti.UI.FILL, data: data, width: Ti.UI.FILL }); win.add(tbl);
Hi Takumi
This has actually become much easier and much more efficient of resources.
Use the new function getRect(). For example;
Ti.API.info(label.getRect().height);See this documentation
label.addEventListener('postlayout', function(e) { // not called ... Ti.API.info(e.source.rect.height); });Is the correct one, you need to remove
toImage() or call it after postlayout.
Hi Takumi
The problem might relate to the fact you have more than just a label on the window and because of this other views updates are effecting your label and how it reports back.
Try moving the postlayout event to the parent, or even the window.
win1.addEventListener('postlayout', function(e) { Ti.API.info(label.getRect().height); });Chances are this may fire more than once, but the answer should be right.
You can reduce the number of times it fires by adding this at the start
win1.startLayout();and this at the end.
win1.finishLayout();
Your Answer
Think you can help? Login to answer this question!