Change font size of UISegmentedControl

108,747

Solution 1

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

Solution 2

Use the Appearance API in iOS 5.0+:

[[UISegmentedControl appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIFont fontWithName:@"STHeitiSC-Medium" size:13.0], UITextAttributeFont, nil] forState:UIControlStateNormal];

Links: http://developer.apple.com/library/ios/#documentation/UIKit/Reference/UIAppearance_Protocol/Reference/Reference.html#//apple_ref/doc/uid/TP40010906

http://www.raywenderlich.com/4344/user-interface-customization-in-ios-5

Solution 3

Here is a Swift version of the accepted answer:

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)

Solution 4

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

Solution 5

Swift Style:

UISegmentedControl.appearance().setTitleTextAttributes(NSDictionary(objects: [UIFont.systemFontOfSize(14.0)], forKeys: [NSFontAttributeName]), forState: UIControlState.Normal)
Share:
108,747

Related videos on Youtube

Aashutosh Tiwari
Author by

Aashutosh Tiwari

Updated on July 08, 2022

Comments

  • Aashutosh Tiwari
    Aashutosh Tiwari almost 2 years

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

    • oky_sabeni
      oky_sabeni over 9 years
      Any solutions in IB instead of code?
    • Fattie
      Fattie over 2 years
      you can not do it in IB. the current 2021 syntax is: UISegmentedControl.appearance().setTitleTextAttributes([NSAt‌​tributedString.Key.f‌​ont: _font], for: .normal)
  • scooter133
    scooter133 over 12 years
    This works great, though if I have already done a [mySegmentedControl setTintColor:onColor forTag:kTagOnState]; and a [mySegmentedControl setTintColor:offColor forTag:kTagOffState]; then apply the [mySegmentedControl setTitleTextAttributes:attributes forState:UIControlStateNormal]; then the Colors I just set go away.
  • runmad
    runmad about 12 years
    You need to use dictionaryWithObjects:forKeys instead and create two arrays with your object and key attributes.
  • rakeshNS
    rakeshNS almost 12 years
    available in iOS 5.0 or later
  • Jason Moore
    Jason Moore over 10 years
    in iOS7: NSDictionary *attributes = @{NSFontAttributeName: [UIFont boldsystemFontOfSize:12.0f]};
  • kjoelbro
    kjoelbro over 10 years
    Thanks to this simple working code. Scaling it to .75f in iOS6 provides the best result. If used in a table cell, then add 10 to the height of the cell.
  • Guillaume
    Guillaume over 10 years
    @JasonMoore boldSystemFontOfSize: (capital S for System)
  • Automate This
    Automate This about 10 years
    UITextAttributeFont has been depreciated - use NSFontAttributeName instead.
  • Nat
    Nat over 9 years
    Worth noticing that it will change the font of ALL UISegmentedControls.
  • Mike Gledhill
    Mike Gledhill over 9 years
    Works a treat in iOS 8. Remind me again, WHY is there no "font" attribute...? (Apple has a lot of explaining to do...!)
  • osrl
    osrl about 8 years
    You should use swift style dictionary. [NSFontAttributeName: font]
  • itzmebibin
    itzmebibin over 7 years
    objFont returns nil value.
  • vahotm
    vahotm about 7 years
    This is not the way you change font size on any control in iOS. Affine transform is mostly intended to be used with animations.
  • Michael Peterson
    Michael Peterson over 6 years
    Please don't scale standard controls.
  • L_J
    L_J almost 6 years
    While this code may answer the question, providing information on how and why it solves the problem improves its long-term value
  • Apoorv Khatreja
    Apoorv Khatreja over 5 years
    This is a great way to make sure than an iOS update will break code that you've shipped to your customers. This might work now, but there's absolutely no guarantees that this will continue working in the future.
  • Benny Davidovitz
    Benny Davidovitz over 5 years
    -- update -- NSDictionary *attributes = @{NSFontAttributeName:font}; [mySegmentedcontrol setTitleTextAttributes:attributes forState:UIControlStateNormal];
  • Zaporozhchenko Oleksandr
    Zaporozhchenko Oleksandr over 5 years
    @MichaelPeterson would be nice to have much more customizable standard controls, so nobody needs to do hack like this one.
  • Patrick Perini
    Patrick Perini about 5 years
    You can drop the key down to simply .font as well
  • Coding while Loading
    Coding while Loading almost 5 years
    You actually have to call it like this let font = UIFont.systemFont(ofSize: 16) UISegmentedControl.appearance().setTitleTextAttributes([NSAt‌​tributedString.Key.f‌​ont: font], for: .normal)
  • Sky
    Sky over 4 years
    Error : Instance member 'setTitleTextAttributes' cannot be used on type 'UISegmentedControl'; did you mean to use a value of this type instead? iOS13/Swift5
  • Fattie
    Fattie over 2 years
    this answer is wrong in 2021