Rishil Patel
Rishil Patel

Reputation: 1995

How to change text color of label in segment controller?

I want to set different text color of label in each row SegmentControl programmatically.

Please check my ref. code.

var arrColors = [
    {"color":"white"},
    {"color":"orange"},
    {"color":"blue"},
    {"color":"yellow"},
    {"color":"gray"}
]; 
this.view.segCont.widgetDataMap = {lblColorName: "color"};
this.view.segCont.setData(arrColors);    

I want to do something like attached image.

Thanks in advance!!

Upvotes: 2

Views: 1170

Answers (2)

Rishil Patel
Rishil Patel

Reputation: 1995

I got solution from kony team.

1) Create different skin for different color label. See below image:

enter image description here

2) Set condition for as per your require color label.

var arrColors = [
    {"color": "white"},
    {"color": "orange"},
    {"color": "blue"},
    {"color": "yellow"},
    {"color": "gray"}
];

for (i = 0; i < arrColors.length; i++) {
    if (arrColors[i].color === "orange") {
        arrColors[i].color = {
            "skin": "sknLblOrange"
        };
    } else {
        arrColors[i].color = {
            "skin": "sknLblGreen"
        };
    }
}

this.view.segCont.widgetDataMap = {
    lblColor: "color"
};
this.view.segCont.setData(arrColors);

Hope this helpful to you. Happy Coding :)

Upvotes: 2

Mig82
Mig82

Reputation: 5480

This is fine if your data is finite and static, or if the data array is always the same length, like with a menu.

However, if your data is dynamic you should consider instead this solution:

var arrColors = [
    {"skin": "whiteRowSkin"},
    {"skin": "orangeRowSkin"},
    {"skin": "blueRowSkin"},
    {"skin": "yellowRowSkin"},
    {"skin": "grayRowSkin"}
];

this.view.segCont.widgetDataMap = {
    lblColor: "color"
    // plus any other properties you need for this data.
};

// Lets assume this getData function fetches your dynamic data from a service.
var segData = getData();

for (var i = 0; i < segData.length; i++) {
    var colorIndex = i % arrColors.length;
    segData[i].color = arrColors[colorIndex];
};

this.view.segCont.setData(segData);

The key above is the Modulus/Remainder % operator, which allows you to decide dynamically which of the colors/skins in the skin array to corresponds to each data row, even if the size of the data array varies.

Note: This obviates the fact that the data may be a matrix if you're using segment sections.

Upvotes: 0

Related Questions