UISegmentedControl选定和未选定段的不同字体颜色

时间:2013-01-07 11:58:42

标签: iphone uisegmentedcontrol

  

可能重复:
  How to change font color of UISegmentedControl

是否可以为selected的{​​{1}}和unselected segment的文字保留不同的字体颜色。任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:3)

// Instantiate as usual
NSArray *items = [NSArray arrayWithObjects:@"first", @"second", [UIImage imageNamed:@"image.png"], nil];
MCSegmentedControl *segmentedControl = [[MCSegmentedControl alloc] initWithItems:items];

// set frame, add to view, set target and action for value change as usual
segmentedControl.frame = CGRectMake(10.0f, 10.0f, 300.0f, 44.0f);
[self.view addSubview:segmentedControl];
[segmentedControl addTarget:self action:@selector(segmentedControlDidChange:) forControlEvents:UIControlEventValueChanged];

// Set a tint color
segmentedControl.tintColor = [UIColor orangeColor];

// Customize font and items color
segmentedControl.selectedItemColor = [UIColor yellowColor];
segmentedControl.unselectedItemColor = [UIColor darkGrayColor];

如果使用Interface Builder,添加普通的UISegmentedControl,在Identity Inspector中将其类设置为MCSegmentedControl,在Attributes Inspector中设置Tint。

目前,不支持动画和以下UISegmentedControl方法:

- (void)setWidth:(CGFloat)width forSegmentAtIndex:(NSUInteger)segment;
- (void)setContentOffset:(CGSize)offset forSegmentAtIndex:(NSUInteger)segment

以下是文件:

MCSegmentedControl.zip