0
votes
for ( i = 0; i < 10; i++) {
    var YPos = 30;   

    var Label1 = Ti.UI.createLabel({
            left : 0,
            top : YPos ,
            width: "50%",
            backgroundColor: "blue",
            height: 20,
            text: i.toString(),
            textAlign: Ti.UI.TEXT_ALIGNMENT_CENTER
        });

    var Label2 = Ti.UI.createLabel({
            left : "50%",
            top : YPos ,
            width: "50%",
            backgroundColor: "blue",
            height: 20,
            text: i.toString(),
            textAlign: Ti.UI.TEXT_ALIGNMENT_CENTER
        });

     YPos += 30;
};

I would not like to use again and again the following block of code in loop.

width: "50%",
backgroundColor: "blue",
height: 20,
text: i.toString(),
textAlign: Ti.UI.TEXT_ALIGNMENT_CENTER

I am using Alloy. I assigned as class as below

var Label2 = Ti.UI.createLabel({
            left : "50%",
            top : YPos ,
            class:"CommonPro"
        });

in .tss file

    ".CommonPro":
   {
           width: "50%",
           backgroundColor: "blue",
           height: 20,
           text: "Bla Bla",
           textAlign: Ti.UI.TEXT_ALIGNMENT_CENTER
    }

Also i used to className:"CommonPro" properties for label but nothing changed.

How approach must i try?

Thanks in advance.

2

2 Answers

1
votes

Try the following

var labelOptions = {
     left : 0,
     width: "50%",
     backgroundColor: "blue",
     height: 20,
     textAlign: Ti.UI.TEXT_ALIGNMENT_CENTER,
     top  : 0,
     text : ''
};
for ( i = 0; i < 10; i++) {
    var YPos = 30;   
    labelOptions.top = YPos;
    labelOptions.string = i.toString();
    var Label1 = Ti.UI.createLabel(labelOptions);
    var Label2 = Ti.UI.createLabel(labelOptions);
    YPos += 30;
}
0
votes

class only supports when you use in xml file so try to create a common json object and pass that into label or create function which returns a common label object.