在UISegmentedcontrol中更改textColor

时间:2010-10-27 10:15:02

标签: iphone xcode uisegmentedcontrol ios4

我使用下面的代码来更改UISegmentedcontrol中每个分段的文本颜色如何更改特定段的文本颜色?(例如索引2处的段)

for (id seg in [segment subviews]) 
     for (id label in [seg subviews]) 
        if ([label isKindOfClass:[UILabel class]])
                [label setTextColor:[UIColor redColor]];

2 个答案:

答案 0 :(得分:2)

我使用下面的代码来解决这个问题

int eg=0;
    for (id seg in [sg subviews]) 
    {
        int gg=sg.selectedSegmentIndex;
        if(gg==2)
            gg=0;
        else if(gg==0)
            gg=2;
        if(eg==gg && eg!=1)
        {
            for (id label in [seg subviews]) 
                if ([label isKindOfClass:[UILabel class]])
                {
                    [label setTextAlignment:UITextAlignmentCenter];
                    [label setFont:[UIFont boldSystemFontOfSize:12]];
                    [label setTextColor:[UIColor colorWithRed:0.325 green:0.565 blue:0.788 alpha:1]];
                }//if
        }//if
        else if(eg==1)
        {
            for (id label in [seg subviews]) 
                if ([label isKindOfClass:[UILabel class]])
                {
                    [label setTextAlignment:UITextAlignmentCenter];
                    [label setFont:[UIFont boldSystemFontOfSize:11]];
                    [label setTextColor:[UIColor grayColor]];   
                }//if
        }//else if
        else
        {
            for (id label in [seg subviews]) 
                if ([label isKindOfClass:[UILabel class]])
                {
                    [label setTextAlignment:UITextAlignmentCenter];
                    [label setFont:[UIFont boldSystemFontOfSize:11]];
                    [label setTextColor:[UIColor colorWithRed:0.888 green:0.888 blue:0.888 alpha:1]];   
                }//if
        }//else
        eg++;
    }//for

答案 1 :(得分:0)

据我所知,没有基于定制的解决方案。但您可以将图像用于必要的细分(setImage:forSegmentAtIndex:)或查看: http://matteocaldari.it/2010/05/a-uisegmentedcontrol-with-custom-color(作者已经将UISegmentedControl子类化,并在覆盖drawRect:方法中实现了自定义绘图。)