首页 文章

如何更改UISegmentedControl的字体颜色

提问于
浏览
54

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 的文字颜色?

10 回答

  • 4

    迅捷3.2:

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

    注意:如果您使用自定义背景颜色,您将在角落中看到一个小故障(颜色将填充外部区段..),因此请使用这些线条剪辑它:

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

    更新为Swift 4 - 使用此扩展(因为扩展总是很棒.. !!)

    extension UISegmentedControl {
    
    func defaultConfiguration(font: UIFont = UIFont.systemFont(ofSize: 12), color: UIColor = UIColor.gray) {
        let defaultAttributes = [
            NSAttributedStringKey.font.rawValue: font,
            NSAttributedStringKey.foregroundColor.rawValue: color
        ]
        setTitleTextAttributes(defaultAttributes, for: .normal)
    }
    
    func selectedConfiguration(font: UIFont = UIFont.boldSystemFont(ofSize: 12), color: UIColor = UIColor.red) {
        let selectedAttributes = [
            NSAttributedStringKey.font.rawValue: font,
            NSAttributedStringKey.foregroundColor.rawValue: color
        ]
        setTitleTextAttributes(selectedAttributes, for: .selected)
    }
    }
    

    从各自的类,你可以使用这些功能,

    @IBOutlet weak var segment: UISegmentedControl!
    
    segment.defaultConfiguration()
    // or provide paramater as per your requirement
    segment.selectedConfiguration(color: .blue)
    
  • 63

    在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];
    
  • 1

    如果您需要更改iOS 7中突出显示的片段的文本颜色,这里有一个解决方案(花了我一段时间才找到,但thanks to this post):

    Objective-C

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

    Swift

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

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

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

    我希望它有所帮助

  • 7

    Swift 4代码将状态字体颜色设置为黑色

    let titleTextAttributes = [NSAttributedStringKey.foregroundColor: UIColor.black]
        segmentedControl.setTitleTextAttributes(titleTextAttributes, for: .normal)
        segmentedControl.setTitleTextAttributes(titleTextAttributes, for: .selected)
    
  • 29
    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及更高版本

  • 115

    以防万一到达那里,并使用swift工作 .

    我会提出两种可行的方法 . 您可以更改 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)
    
  • 4

    在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部分中找到所有可能的简单自定义 .

  • 4

    我正在使用monotouch . 不知道为什么,但是当推送View时,文字颜色对我来说没有变化 . 为了解决这个问题,我只需在分段控制超视图中添加标签,然后更改颜色:

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

相关问题