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.
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
-
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.
-
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.
-
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);