Skip to content Skip to sidebar Skip to footer

How To Get The Actual Height Of A Label With Auto-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

Solution 1:

You 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. :)

Solution 2:

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 compared 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.

Post a Comment for "How To Get The Actual Height Of A Label With Auto-height"