如何更改UISegmentedControl的字体颜色 [英] How to change font color of UISegmentedControl

查看:311
本文介绍了如何更改UISegmentedControl的字体颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试将 UISegmentedControl (适用于iOS 4。*)的字体颜色从白色更改为黑色。

I try to change font color from white to black for UISegmentedControl (for iOS 4.*)

UISegmentedControl *button = [[[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:itemTitle, nil]] autorelease];
button.momentary = YES;
button.segmentedControlStyle = UISegmentedControlStyleBar;
button.tintColor = [UIColor redColor];      
for (id segment in [button subviews]) {
    for (id label in [segment subviews]) {
        if ([label isKindOfClass:[UILabel class]]) {
            UILabel *titleLabel = (UILabel *) label;
            [titleLabel setTextColor:[UIColor blackColor]];
        }
    }
}
UIBarButtonItem *barButtonItem = [[[UIBarButtonItem alloc] initWithCustomView:button] autorelease];

但文字颜色不变。我应该为 UISegmentedControl 更改文字颜色做什么?

But text color does not changed. What I should do for change text color for UISegmentedControl?

推荐答案

iOS 5.0及以上版本很简单:

In iOS 5.0 and above it's very simple:

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];

这篇关于如何更改UISegmentedControl的字体颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆