更改 UISegmentedcontrol 中的文本颜色

发布于 2024-09-29 11:56:30 字数 285 浏览 3 评论 0原文

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

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

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)

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

别靠近我心 2024-10-06 11:56:30

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

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

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
时光无声 2024-10-06 11:56:30

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

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).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文