I need to create a label with a default text, example:
var myLabel = Ti.UI.createLabel({ text: "some text", height: 'auto', width: 'auto' });And then when I need to change the text like
myLabel.setText("some bigger text here");I need the label to be resized (width and height) to display the entire text, but I cant get to do that. Also creating another label to replace the existing one would not be good because its inside a tableView so I would have to delete all rows and append again.
4 Answers
Accepted Answer
Heres the working code. You need to set the height and width on the row too.
var win = Ti.UI.createWindow({ width: '100%', height: '100%' }); var tableview = Ti.UI.createTableView({ width: '100%', height: '100%' }); var row = Ti.UI.createTableViewRow({ }); var label = Ti.UI.createLabel({ text: "some text", backgroundColor: 'gray', height: Ti.UI.SIZE, width: Ti.UI.SIZE }); row.add(label); tableview.appendRow(row); win.add(tableview); function randomString(bottom, top) { if (bottom >= top) return null; var text = ""; var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvxwyz0123456789"; var len = Math.floor(Math.random() * (top - bottom)) + bottom; for( var i=0; i < len; i++ ) text += possible.charAt(Math.floor(Math.random() * possible.length)); return text; } function changeText() { label.text = randomString(10,40) label.width = 100; label.height = 50; row.width = 100; row.height = 50; } setInterval(changeText, 1000); win.open();
Have you tried using Ti.UI.SIZE for the width and height parameters?
So lets say you are changing the text of the label on some event, then when you change the text, you can also adjust the height and width at the same time.
If the new text is known in advance, you can have fixed values for the width and height, else you can come up with a small formula depending on the number of characters in your message.
Here is the example I'm working on
var win = Ti.UI.createWindow({ width: '100%', height: '100%' }); var tableview = Ti.UI.createTableView({ width: '100%', height: '100%' }); var row = Ti.UI.createTableViewRow({ }); var label = Ti.UI.createLabel({ text: "some text", backgroundColor: 'gray', height: Ti.UI.SIZE, width: Ti.UI.SIZE }); row.add(label); tableview.appendRow(row); win.add(tableview); function randomString(bottom, top) { if (bottom >= top) return null; var text = ""; var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvxwyz0123456789"; var len = Math.floor(Math.random() * (top - bottom)) + bottom; for( var i=0; i < len; i++ ) text += possible.charAt(Math.floor(Math.random() * possible.length)); return text; } function changeText() { label.text = randomString(10,40) } setInterval(changeText, 1000); win.open();
Your Answer
Think you can help? Login to answer this question!