Titanium Community Questions & Answer Archive

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

Label w auto height disappears

I'm building a grouped table view from a database table. Each row has a pair of labels with height set to auto on each as well as the row. When I use the size.height property of the label it causes them to randomly disappear. Also if you click on that row sometimes the label then displays.

        var row = Titanium.UI.createTableViewRow({
            height:'auto',
            selectedBackgroundColor:'transparent'
        });
        var rowLabel1 = Ti.UI.createLabel({
            text:'Section: ' + s + ', Row: ' + r,
            color:'#576996',
            font:{fontSize:14,fontWeight:'bold'},
            top:rowPadding,
            height:'auto',
            width: 300 - (rowPadding * 2),
            left:rowPadding
        });
        row.add(rowLabel1);

        var rowLabel2 = Ti.UI.createLabel({
            text:"Other textnAnother line of text",
            font:{fontSize:14},
            top:rowLabel1.size.height + rowPadding,
            height:'auto',
            left:rowLabel1.left,
            width:rowLabel1.width
        });
        row.add(rowLabel2);

        var textHeight = rowLabel1.size.height + rowLabel2.size.height;
        row.height = textHeight + (rowPadding * 2);

I tried remove the .size.height calls and removing the formatting by reducing it to just one label. All the labels show up correctly now, but sometimes the spacing is off, especially after scrolling.

for (var s=1;s<5;s++) {
    for (var r=1;r<10;r++) {
        var row = Titanium.UI.createTableViewRow({
            height:'auto',
            selectedBackgroundColor:'transparent'
        });
        var title = 'Section: ' + s + ', ';
        if (s%2==0) title = title + "n";
        title = title + 'Row: ' + r;
        var rowLabel1 = Ti.UI.createLabel({
            text:title + "nOther textnAnother line of text",
            color:'#576996',
            font:{fontSize:14,fontWeight:'bold'},
            top:rowPadding,
            height:'auto',
            bottom:rowPadding,
            right:rowPadding,
            left:rowPadding
        });
        row.add(rowLabel1);
        if (r==1) {
            row.header = 'Section: ' + s;
        }
        data.push(row);
    }
}
— asked March 25th 2010 by Kurt Gailey
  • 1.1.0
  • height
  • iphone
  • label
  • mobile
0 Comments

3 Answers

  • I'm having the exact same issue with disappearing labels on a View. I've gotten around the issue by creating a "dummy" label first to get the height and then creating the "real" label with a fixed height:

    var label = Titanium.UI.createLabel({
       text: description,
       width:100,
       height:'auto',
       top:10,
       left:10
    });
    var label2 = Titanium.UI.createLabel({
       text: description,
       width:100,
       height:label.size.height,
       top:10,
       left:10
    });
    view.add(label2);
    

    Not ideal, I know - but it seems to keep the labels visible.

    — answered April 29th 2010 by Briley Hooper
    permalink
    3 Comments
    • That doesn't work for me…
      Somehow label.size.height returns 0.0

      — commented February 3rd 2011 by Léon Doornkamp
    • This solution worked for me on iPhone, but not on Android

      — commented October 21st 2011 by Andres Vega
    • size is only populated after displaying the label. It means, that you first have to add label to some view (or window), and show it.

      — commented January 6th 2012 by Kristof Gruber
  • label.size.height is working only for iphone and its not working for android

    — answered November 14th 2011 by sridhar davuluri
    permalink
    1 Comment
    • This seems to be the case. I am currently struggling with a way to fix this. Works great on iPhone, but I can not get label.size.height to return a value on Android, and setting height to 'auto' is also not working, resulting in a totally busted tableview on Android.

      — commented November 15th 2011 by David Richardson
  • Hello,

    i have the same problem can you help me to fix it?
    do you have any solution?

    thanks,
    Eliza

    — answered February 22nd 2012 by Eliza Sapir
    permalink
    0 Comments
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.