Titanium Community Questions & Answer Archive

We felt that 6+ years of knowledge should not die so this is the Titanium Community Questions & Answer Archive

Retrieving height of labels after setting text value

Hi all,

there's a similar post about but doesn't seems to work… what I need is found a way to retrieve the height of a label with a height set to 'auto' after setting its text value.

this is my code: http://pastie.org/private/fev45vx6nv2dprd7ruwprw

If I try to get the textip.size.height it always return 0

I need this info to position another label with text just under the previous one…

Thanks a lot!

— asked June 1st 2010 by Joe Maffia
  • createlabel
  • createtableviewrow
  • height
  • label
1 Comment
  • Same problem here…

    — commented June 10th 2010 by Markus Birth

5 Answers

  • I've got exactly the same issue. Other threads haven't solved it.

    Here's some stuff I've learned which you may be able to use as a way around it until Appcelerator fix this.

    • You can't use label.size.height on Android until after you've added the label to the current window.

    • when using label.toImage().height on iPhone for labels where there are several wrapped lines of text, it's not very accurate.

    I'm considering setting the label height by working out how many characters you can get on one line of the screen width (var lnw), then dividing the text.length (txtln) by this number to get the number of lines of text to put in the label (numlns), then multiplying that figure by the font size of the label (fnsz):

    // chars per line determined from some manual tests
    var charsPerLn = 120; // no idea what this actually is yet
    // text to put into the label
    var txt = "blah blah blah...";
    // length of the text
    var txtln = txt.length;
    // rows
    var numlns = txtln / charsPerLn;
    var lbl = Ti.UI.createLabel({width:'auto',font:{fontSize:12}});
    lbl.height = numlns * lbl.font.fontSize;

    Can't quite bring myself to do this - it's a bit messy because characters are different widths. Might have to add a line (numlns++) as an overflow buffer, but then you'll end up with a space before the next element.

    This bug is actually ridiculous, considering the positioning of elements is 'absolute' in css terms.

    I can't find any examples of dynamic label height in KitchenSink.

    This is a big concern in my investigation into using titanium for our company or not.

    — answered August 10th 2010 by John Kirby
  • 1.4 fixed getting the actual height for some UI elements, but I haven't tried it with a label.

    A workaround that has worked for me in general in the past is adding the label to the window somewhere with label.visible = false, grab the height, remove it, and put it where you want it to go.

    In general, I've noticed that when you have auto height, it helps to set a fixed width if possible.

    — answered August 10th 2010 by Rob Marscher
    1 Comment
    • Thanks for the idea Rob.

      I made a small fix function anyone with this problem could use.

      Get width of elements on Android fix
      function fix_width(element)
          element.visible = false;
          element.visible = true;

      And when you need the width call it like this:

      if(Ti.Platform.name == "android") { fix_width(label_element); }

      Now the width is available through:


      Could change it for height off course!

      — commented July 20th 2011 by Eric Beekman
  • I figured out a workaround for it… Once at home I'll paste my solution, hope it will help.


    There u go in short my version:


    — answered August 10th 2010 by Joe Maffia
  • On iOS it seems that the label returns correct size after it has been added on a view that has been added to the main stage.

    On Android this property returns incorrect values. The only workaround I have found so far was to remove the label from the view, set the text, add it back and then voila, the size returns correct values.

    label.text = 'This is your text contents\nwith some newlines';
    Ti.API.info('Label size is now correct: '+JSON.stringify(label.size));
    — answered April 19th 2012 by Ondrej Urik
  • Is there a way to access the height of a label in alloy? The suggested method by Eric does not work unfortunately.

    — answered February 9th 2013 by Norman Dilthey
The ownership of individual contributions to this community generated content is retained by the authors of their contributions.
All trademarks remain the property of the respective owner.