It seems that if you set the width of a label to a percentage value, Titanium drops off the % and treats as pixels. Has anyone else seen this?
var lbl = Ti.UI.createLabel({ text:'test', width:'45%' }); Ti.API.debug('lbl.width = ' + lbl.width);Results in:
[DEBUG] lbl.width = 45
2 Answers
Accepted Answer
Hello Phil,
Hm.. indeed you are right. I have tried various examples and it all ends up into the conclusion that the property lbl.width or even the lbl.getWidth() is returned as a number. I tried various examples out of curiosity, like yours, but also the following:
var myWidth = "45%"; var lbl = Ti.UI.createLabel({ text:'test', width:myWidth });lbl.width returns 45. lbl.getWidth() returns 45.
performing the test alert(typeof lbl.width) returns 'number' whereas the test alert(typeof lbl.myWidth) returns 'string'.
The good news is that the result is the desired, i.e. your label is expanded up to 45%. It looks like a bug. Now, the only way to work around this is to create a new object that would handle both the width value and type. Consider the following constructor.
function width(arg) { this.width = arg; this.isPercentage = function() { if(arg.indexOf('%') != -1) { return true; } else { return false; } } }now, should you wish to use a percentage for a label width, I would do this:
myWidth = new width("10%"); var lbl = Ti.UI.createLabel({ text:'test', width:myWidth });and of course, to get the width type and do your job, try this: alert(myWidth.isPercentage());
duuh ... need an easier way? ignore the constructor. Go for this
var myWidth = "10%"; var lbl = Ti.UI.createLabel({ text:'test', width:myWidth });now, if you wish to get the 10% back, just get it from your initial variable `myWidth' and not from what is returned.
I hope they will fix this soon..
George
Did you try lbl.toImage().width ?
var win = Ti.UI.createWindow({backgroundColor: 'white'}); var lbl = Ti.UI.createLabel({ text:'test', width:'50%' }); win.add(lbl); win.open(); alert('lbl.width = ' + lbl.toImage().width);
Your Answer
Think you can help? Login to answer this question!