Change font size of UISegmentedControl

前端 未结 16 1748
死守一世寂寞
死守一世寂寞 2020-11-28 20:54

Can anyone please tell me how can I change the font type and size of UISegmentedControl?

相关标签:
16条回答
  • 2020-11-28 21:05

    Swift 4

    let font = UIFont.systemFont(ofSize: 16)
    UISegmentedControl.setTitleTextAttributes([NSFontAttributeName: font], for: .normal)
    
    0 讨论(0)
  • 2020-11-28 21:05

    this is for objective c add your segmented control name in place of mysegmentedcontrol

    UIFont *font = [UIFont systemFontOfSize:11.0f];
    
    NSDictionary *attributes = [NSDictionary dictionaryWithObject:font
                                                                forKey:UITextAttributeFont];
    
    [mySegmentedcontrol setTitleTextAttributes:attributes                                    forState:UIControlStateNormal];
    

    hope it helps

    0 讨论(0)
  • 2020-11-28 21:07

    I ran into the same issue. This code sets the font size for the entire segmented control. Something similar might work for setting the font type. Note that this is only available for iOS5+

    Obj C:

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

    EDIT: UITextAttributeFont has been deprecated - use NSFontAttributeName instead.

    EDIT #2: For Swift 4 NSFontAttributeName has been changed to NSAttributedStringKey.font.

    Swift 5:

    let font = UIFont.systemFont(ofSize: 16)
    segmentedControl.setTitleTextAttributes([NSAttributedString.Key.font: font], for: .normal)
    

    Swift 4:

    let font = UIFont.systemFont(ofSize: 16)
    segmentedControl.setTitleTextAttributes([NSAttributedStringKey.font: font],
                                            for: .normal)
    

    Swift 3:

    let font = UIFont.systemFont(ofSize: 16)
    segmentedControl.setTitleTextAttributes([NSFontAttributeName: font],
                                            for: .normal)
    

    Swift 2.2:

    let font = UIFont.systemFontOfSize(16)
    segmentedControl.setTitleTextAttributes([NSFontAttributeName: font], 
        forState: UIControlState.Normal)
    

    Thanks to the Swift implementations from @audrey-gordeev

    0 讨论(0)
  • 2020-11-28 21:09

    Daniel pointed me to correct way. I used it like this-

    float scaleFactor = 0.8f;
    
    UISegmentedControl *segmentedControl = [[UISegmentedControl alloc]
    initWithFrame:CGRectMake(10, 70, 300/scaleFactor,35)];
    
    [segmentedControl insertSegmentWithTitle:@"..." atIndex:0 animated:NO];
    [segmentedControl insertSegmentWithTitle:@"..." atIndex:1 animated:NO];
    [segmentedControl insertSegmentWithTitle:@"..." atIndex:2 animated:NO];
    
    segmentedControl.transform = CGAffineTransformMakeScale(scaleFactor, 1);
    CGPoint segmentedControlCenter = segmentedControl.center;
    segmentedControlCenter.x = self.center.x;
    segmentedControl.center = segmentedControlCenter;
    
    0 讨论(0)
  • 2020-11-28 21:11

    Another option is to apply a transform to the control. However, it will scale down everything including the control borders.

    segmentedControl.transform = CGAffineTransformMakeScale(.6f, .6f);
    
    0 讨论(0)
  • 2020-11-28 21:16
    // Set font-size and font-femily the way you want
    UIFont *objFont = [UIFont fontWithName:@"DroidSans" size:18.0f];
    
    // Add font object to Dictionary
    NSDictionary *dictAttributes = [NSDictionary dictionaryWithObject:objFont forKey:NSFontAttributeName];
    
    // Set dictionary to the titleTextAttributes
    [yourSegment setTitleTextAttributes:dictAttributes forState:UIControlStateNormal];
    

    If you have any query, Contact me.

    0 讨论(0)
提交回复
热议问题