Label Width ignores Percent Sign

You must Login before you can answer or comment on any questions.

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

— answered 1 year ago by George Georgiou
answer permalink
2 Comments
  • Thanks for confirming this for me George!

    For me the issue is that the width doesn't seem to be scale. In fact, the labels weren't being displayed at all. Since they are part of a larger custom control, I'm processing the incoming arguments looking for the percentage values and calculating the width based on the parent control, and then using that to create the labels.

    Mostly, I wanted to confirm that I wasn't missing something. I didn't see anything quite like this in JIRA, so I'll try to submit a bug in the near future.

    — commented 1 year ago by Phil Sweeney

  • If nothing shows up I would recommend to play around with your container's layout value (horizontal, vertical, absolute) but even most important, try to set a height in your labels to see if they show up!

    — commented 1 year ago by George Georgiou

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!