如何更改UISegmentedControl的字体颜色

我尝试将UISegmentedControl字体颜色从白色更改为黑色(对于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文字颜色?

在iOS 6.0及以上版本中,这非常简单:

 NSDictionary *attributes = [NSDictionary dictionaryWithObjectsAndKeys: [UIFont boldSystemFontOfSize:17], NSFontAttributeName, [UIColor blackColor], NSForegroundColorAttributeName, nil]; [_segmentedControl setTitleTextAttributes:attributes forState:UIControlStateNormal]; NSDictionary *highlightedAttributes = [NSDictionary dictionaryWithObject:[UIColor whiteColor] forKey:NSForegroundColorAttributeName]; [_segmentedControl setTitleTextAttributes:highlightedAttributes forState:UIControlStateSelected]; 

如果您需要更改iOS 7中突出显示段的文本颜色,请使用以下解决scheme(花了我一些时间寻找,但是感谢这篇文章 ):

Objective-C的

 [[UISegmentedControl appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColor blackColor]} forState:UIControlStateSelected]; 

迅速

  let titleTextAttributes = [NSForegroundColorAttributeName: UIColor.whiteColor()] UISegmentedControl.appearance().setTitleTextAttributes(titleTextAttributes, forState: .Selected) 

以下是将字体设置为粗体和点大小的代码:

 UIFont *Boldfont = [UIFont boldSystemFontOfSize:12.0f]; NSDictionary *attributes = [NSDictionary dictionaryWithObject:Boldfont forKey: NSFontAttributeName]; [segmentedControl setTitleTextAttributes:attributes forState:UIControlStateNormal]; 

我希望它有帮助

 for (UIView *v in [[[segment subviews] objectAtIndex:0] subviews]) { if ([v isKindOfClass:[UILabel class]]) { UILabel *label=(UILabel *)[v retain]; lable.textColor=[UIColor blackColor]; } } 

适用于iOS 3.0及以上版本

以防万一,以帮助其他人到达那里,正在使用迅速。

我会把这两种可能的方式做到这一点。 您可以更改UIAppearance的文本属性,或直接在分段中工作。

第一个示例在外观中设置属性,这样您将自定义应用程序中的所有分段控件:

  let attributes = [ NSForegroundColorAttributeName : UIColor.grayColor(), NSFontAttributeName : UIFont.systemFontOfSize(20)]; let attributesSelected = [ NSForegroundColorAttributeName : UIColor.blueColor(), NSFontAttributeName : UIFont.systemFontOfSize(20)]; UISegmentedControl.appearance().setTitleTextAttributes(attributes, forState: UIControlState.Normal) UISegmentedControl.appearance().setTitleTextAttributes(attributesSelected, forState: UIControlState.Selected) 

第二个例子,直接在分段,将只定制这个分段:

  let attributes = [ NSForegroundColorAttributeName : UIColor.grayColor(), NSFontAttributeName : UIFont.systemFontOfSize(20)]; let attributesSelected = [ NSForegroundColorAttributeName : UIColor.blueColor(), NSFontAttributeName : UIFont.systemFontOfSize(20)]; segmented.setTitleTextAttributes(attributes, forState: UIControlState.Normal) segmented.setTitleTextAttributes(attributesSelected, forState: UIControlState.Selected) 

在iOS 5.0及更高版本中,您可以使用titleTextAttributes来自定义UISegmentedControl对象:

 NSDictionary *segmentedControlTextAttributes = @{NSFontAttributeName:[UIFont fontWithName:@"HelveticaNeue" size:18.0], NSForegroundColorAttributeName:[UIColor whiteColor]}; [self.segmentedControl setTitleTextAttributes:segmentedControlTextAttributes forState:UIControlStateNormal]; [self.segmentedControl setTitleTextAttributes:segmentedControlTextAttributes forState:UIControlStateHighlighted]; [self.segmentedControl setTitleTextAttributes:segmentedControlTextAttributes forState:UIControlStateSelected]; 

在这里,我将字体设置为UISegmentedControl每个状态的自定义字体,字体大小和颜色。

您将在UISegmentedControl类参考的Customizing Appearance部分中find所有可能的简单自定义 。

迅捷3.2:

 let attributes = [ NSFontAttributeName : bigTextFont, NSForegroundColorAttributeName : UIColor.blue, ] segmentedControl?.setTitleTextAttributes(attributes, for: .normal) 

注意:如果你使用自定义的背景颜色,你会看到一个angular落里的毛刺(颜色会填满外部片段..),所以使用这些线来剪辑:

 segmentedControl!.layer.cornerRadius = 4.0 segmentedControl!.clipsToBounds = true 

我使用monotouch。 不知道为什么,但当查看推文字颜色不会改变我。 为了解决这个即时通讯只是添加标签段控制超视图,然后改变他们的颜色:

 public static void SetColoredTittles(this UISegmentedControl s, string[] titles, UIColor selected, UIColor notSelected) { var segmentedLabels = new List<UILabel>(); float width = s.Frame.Width/titles.Length; for (int i = 0; i < titles.Length; i++) { var frame = new RectangleF(s.Frame.X + i*width, s.Frame.Y, width,s.Frame.Height); UILabel label = new UILabel(frame); label.TextAlignment = UITextAlignment.Center; label.BackgroundColor = UIColor.Clear; label.Font = UIFont.BoldSystemFontOfSize(12f); label.Text = titles[i]; s.Superview.AddSubview(label); segmentedLabels.Add(label); } s.ValueChanged += delegate { TextColorChange(s,segmentedLabels, selected, notSelected); }; TextColorChange(s,segmentedLabels, selected, notSelected); } static void TextColorChange(UISegmentedControl s, List<UILabel> segmentedLabels, UIColor selected, UIColor notSelected) { for (int i = 0; i < segmentedLabels.Count; i++) { if(i == s.SelectedSegment) segmentedLabels[i].TextColor = selected; else segmentedLabels[i].TextColor = notSelected; } } 

然后使用它

 segmented.SetColoredTittles(new string[] { "text1", "text2", "text3" }, UIColor.White,UIColor.DarkGray);