首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >无法在segmentedControl中更改字体颜色

无法在segmentedControl中更改字体颜色
EN

Stack Overflow用户
提问于 2012-12-13 15:55:37
回答 1查看 126关注 0票数 0

我正在使用下面的代码在我的分段控件中设置文本颜色。然而,它似乎不起作用。我是不是漏掉了什么?

代码语言:javascript
复制
// Set up segment control
NSArray *itemArray = [NSArray arrayWithObjects: @"Popular", @"Starred", nil];
UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] initWithItems:itemArray];
segmentedControl.frame = CGRectMake(40, 200, 220, 20);
segmentedControl.segmentedControlStyle = UISegmentedControlStyleBar;
segmentedControl.selectedSegmentIndex = 0;

self.navigationItem.titleView = segmentedControl;

for (id segment in [segmentedControl subviews])
{
    for (id label in [segment subviews])
    {
        if ([label isKindOfClass:[UILabel class]])
        {
            [label setTextAlignment:UITextAlignmentCenter];
            [label setColor:[UIColor blackColor]];
        }
    }           
}

EN

回答 1

Stack Overflow用户

发布于 2012-12-13 16:16:51

如果你的目标是iOS 5+,那么你应该这样做:

代码语言:javascript
复制
NSDictionary *attributes = [NSDictionary dictionaryWithObjectsAndKeys:
                        [UIFont boldSystemFontOfSize:17], UITextAttributeFont,
                        [UIColor blackColor], UITextAttributeTextColor,
                        nil];
[_segmentedControl setTitleTextAttributes:attributes forState:UIControlStateNormal];
NSDictionary *highlightedAttributes = [NSDictionary dictionaryWithObject:[UIColor whiteColor] forKey:UITextAttributeTextColor];
[_segmentedControl setTitleTextAttributes:highlightedAttributes forState:UIControlStateHighlighted];
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13855380

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档