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!
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.
-
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.
-
I figured out a workaround for it… Once at home I'll paste my solution, hope it will help.
Best.
There u go in short my version:
http://www.pastie.org/private/mvcged5jtsjdiv24oeq
-
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.
view.remove(label); label.text = 'This is your text contents\nwith some newlines'; view.add(label); Ti.API.info('Label size is now correct: '+JSON.stringify(label.size));
-
Is there a way to access the height of a label in alloy? The suggested method by Eric does not work unfortunately.