0

Is it possible to have each segment in a UISegmentedControl's text / font colour to be different?

I see to be able to only set the global colour and this is not what I am looking for.

halfer
  • 18,701
  • 13
  • 79
  • 158
Robert J. Clegg
  • 6,549
  • 8
  • 43
  • 92

1 Answers1

1

Yes, you can set different colours for each segment, try the below code.....

// assuming there are 3 segments

// background colour

for (int i = 0; i < [segmentControl.subviews count]; i++)
{
    UIColor *tintcolor = nil;
    if (i == 0)
        tintcolor = [UIColor blueColor];
    else if (i == 1)
        tintcolor = [UIColor greenColor];
    else if (i == 2)
        tintcolor = [UIColor redColor];
    [segmentControl.subviews[i] setTintColor:tintcolor];
}

// text color

for (id segmentControl in [self.segmentedControl subviews])
{
    for (id label in [segmentControl subviews])
    {
        if ([label isKindOfClass:[UILabel class]])
            [label setTextColor:[UIColor darkGrayColor]];    // here you can set whatever colour you want
    }
}
[segmentControl setNeedsDisplay];
Keshav
  • 1,842
  • 1
  • 19
  • 28