Titanium JIRA Archive
Titanium SDK/CLI (TIMOB)

[TIMOB-15619] toImage does not honor 'width' when rasterizing a Label. Rasterizing to a window works properly.

GitHub Issuen/a
TypeBug
Priorityn/a
StatusOpen
ResolutionUnresolved
Affected Version/sn/a
Fix Version/sn/a
ComponentsiOS
LabelsLabel, getSize, height, toImage, width
ReporterHenry David Spells III
AssigneeUnknown
Created2013-09-10T16:01:05.000+0000
Updated2018-02-28T20:03:20.000+0000

Description

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

Comments

  1. Henry David Spells III 2013-10-02

    This seems to work properly on Android. This is still active in Ti SDK 3.1.3GA on iOS.

JSON Source