xcode 在 UISegmentedcontrol 中更改 textColor

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4032091/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 19:52:58  来源:igfitidea点击:

Change textColor in UISegmentedcontrol

iphonexcodeuisegmentedcontrolios4

提问by SajjadZare

I use below code for change textcolor of each segmented in UISegmentedcontrol how can I change textcolor of specific segment?(for example segment at index 2)

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

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

回答by SajjadZare

i use below code for this problem

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

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

回答by kpower

As far as I know there is no customization-based solution for this. But you can either use images for necessary segments (setImage:forSegmentAtIndex:) or look at this: http://matteocaldari.it/2010/05/a-uisegmentedcontrol-with-custom-color(authors have subclassed UISegmentedControl & implemented custom drawing in overriden drawRect:method).

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