How can I get the width of a label after set its contents? No initial width set to it.
I have two labels within a table row like followings:
label 1 (label 2)
Here the width of label 1 is not set initially. with the change of contents label 1‘s width has changes and sometimes it causes overlapping with label 2.
Now my main aim is to set the label 2 left dynamically that it does not ovelapp with label 1. I want to do it like following:
- first calculate the width of
label 1 - then set the left of
label 2 = width of label 1 + 10
if there is any other way please help.
HERE SOME CODE
var win = Titanium.UI.createWindow({
title: 'Cleos',
backgroundImage: 'tablebg.png',
navBarHidden: true,
});
var tableview = Titanium.UI.createTableView({
backgroundColor: 'transparent',
separatorStyle: Ti.UI.iPhone.TableViewSeparatorStyle.NONE,
style: Titanium.UI.iPhone.TableViewStyle.PLAIN
});
win.add(tableview);
var modelData = new Array;
for (i = 0; i <= daga.length - 1; i++) { // `data` as json, comes from a source
var row = Titanium.UI.createTableViewRow({
height: 120.0,
backgroundColor: 'transparent',
selectedBackgroundColor: '#380710',
hasDetail: true
});
var imageThumb = Titanium.UI.createImageView({
image: '...',
width: 100.0,
height: 100.0,
left: 4.0,
top: 10.0
});
row.add(imageThumb);
var name = Titanium.UI.createLabel({
text: '...',
font: {
fontSize: 16,
fontWeight: 'bold'
},
textAlign: 'left',
color: '#fff',
height: 30.0,
top: 5.0,
left: 110.0
});
row.add(modelname);
var NumberOfImages = Titanium.UI.createLabel({
text: '(' + 12 + ')',
font: {
fontSize: 16
},
width: 'auto',
color: '#bfbebe',
textAlign: 'left',
height: 30.0,
top: 5.0,
left: '' // I want to set this `left = width of label name`
});
row.add(NumberOfImages);
modelData.push(row);
tableview.setData(modelData);
}
win.open();
but be careful with ‘auto’ values since they often return 0. the value should be requested after adding the label to the view.
[update]
if
NumberOfImages.left = name.width+name.left;doesn’t work i would try to calculate the width like