function labelHeight2(sOutText, daFont, iWidth, win, doImageView)
{
if (typeof sOutText === undefined || sOutText === null || sOutText == '')
return 0;
var daLabel = Ti.UI.createLabel(
{
font: daFont,
color: 'black',
backgroundColor:'transparent',
layout: 'vertical',
height: 'auto',
width: iWidth + 'dp',
text: sOutText,
horizontalWrap: true,
textAlign: Ti.UI.TEXT_ALIGNMENT_LEFT,
verticalAlign: Ti.UI.TEXT_VERTICAL_ALIGNMENT_TOP
});
if (doImageView)
{
var blob = daLabel.toImage();
var iWidth = blob.width; // This value return 988 on my machine, it should be 200 or less since I limited the width of the label to 200
Ti.API.info('labelHeight2 width ' + iWidth);
var image = Ti.UI.createImageView({image: blob});
win.add(image);
}
else
win.add(daLabel);
var asize = daLabel.getSize();
var iHeight = asize.height; // Return 30 when rasterizing to image and 150 when rasterizing to a window (150 is the correct answer).
Ti.API.info('labelHeight2 height ' + iWidth);
return iHeight;
}
function doOnOpen()
{
var s = "Now is the time for all good cows to come to the aid of their farmers. Now is the time for all good cows to come to the aid of their farmers. Now is the time for all good cows to come to the aid of their farmers. Now is the time for all good cows to come to the aid of their farmers. Now is the time for all good cows to come to the aid of their farmers.";
var iHeight = labelHeight2(s, {fontFamily: 'Helvetice Neue', fontSize: '12dp', fontWeight: 'normal'}, 200, win, false);
iHeight = labelHeight2(s, {fontFamily: 'Helvetice Neue', fontSize: '12dp', fontWeight: 'normal'}, 200, win, true);
iHeight = labelHeight2('Testing', {fontFamily: 'Helvetice Neue', fontSize: '12dp', fontWeight: 'normal'}, 200, win, false);
}
This seems to work properly on Android. This is still active in Ti SDK 3.1.3GA on iOS.