Titanium Community Questions & Answer Archive

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

Any way to determine the actual height of a Label or ImageView

If I am creating a label where i do not know the length and set height to 'auto'. Is there any way to determine height of the object in the view after it has been added?

The purpose is to be able to display a Title and then an image below the title. But the title could be 1,2, or 3 lines long.

— asked May 10th 2010 by Dan Newman
  • auto
  • formatting
  • height
0 Comments

4 Answers

  • Let me cast a vote for addressing this somehow in the API. If possible - it pretty key for doing anything at all complex/flexible layout wise.

    thx

    — answered May 10th 2010 by Dan Newman
    permalink
    0 Comments
  • for now, the best route to take would be to place the image before the label, especially if the images are going to be roughly the same size height wise.

    — answered May 10th 2010 by Clint Tredway
    permalink
    0 Comments
  • Ok here is a round about way. May not be 100% precise, but it could be close. Count the characters in the label and determine how many characters will fit on each line, then determine from there where the image should go.

    — answered May 10th 2010 by Stan Thompson
    permalink
    0 Comments
  • What I did was this:

    I have a Label and then I count the Length of the Label.text and multiply this value by a given number, like 5 (which represents the width of a letter in pixels) with that I have a approximation of the Length of the Label and I place the Image after this position.

    Here is a sample code:

    //Label with link to open on Safari
    // Add a Button right after the Label to hold the Event
    
    var link = "http://www.apple.com/";
    
    posLeft_btnOpenSafari = (link.length * 5) + 10; //5 is the length in pixels of each character times the number of characters on my Link variable
    
    var LinkPermalinkLabel = Titanium.UI.createLabel({
        color:'#FFF',
        text: link ,
        textAlign:'left',
        font: {
            fontSize:14,
            fontFamily:'Georgia',
            fontStyle: 'italic',
            fontWeight: 'bold'
        },
        bottom:35,
        left:73,
        width:500,
        height:15
    });
    
    win.add(LinkPermalinkLabel);
    
    var btn_openSafari = Titanium.UI.createButton({
        backgroundImage:'images/btn_fwd.png',
        width:22,
        height:17,
        bottom: 35,
        left: posLeft_btnOpenSafari // here I use my Relative position
    });
    win.add(btn_openSafari);
    
    — answered October 19th 2010 by Antonio Silveira
    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.