Change font size of UISegmentedControl
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];
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)
Related videos on Youtube
Aashutosh Tiwari
Updated on July 08, 2022Comments
-
Aashutosh Tiwari almost 2 years
Can anyone please tell me how can I change the font type and size of
UISegmentedControl
?-
oky_sabeni over 9 yearsAny solutions in IB instead of code?
-
Fattie over 2 yearsyou can not do it in IB. the current 2021 syntax is: UISegmentedControl.appearance().setTitleTextAttributes([NSAttributedString.Key.font: _font], for: .normal)
-
-
scooter133 over 12 yearsThis 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 about 12 yearsYou need to use dictionaryWithObjects:forKeys instead and create two arrays with your object and key attributes.
-
rakeshNS almost 12 yearsavailable in iOS 5.0 or later
-
Jason Moore over 10 yearsin iOS7:
NSDictionary *attributes = @{NSFontAttributeName: [UIFont boldsystemFontOfSize:12.0f]};
-
kjoelbro over 10 yearsThanks 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 over 10 years@JasonMoore
boldSystemFontOfSize:
(capital S for System) -
Automate This about 10 years
UITextAttributeFont
has been depreciated - useNSFontAttributeName
instead. -
Nat over 9 yearsWorth noticing that it will change the font of ALL
UISegmentedControl
s. -
Mike Gledhill over 9 yearsWorks a treat in iOS 8. Remind me again, WHY is there no "font" attribute...? (Apple has a lot of explaining to do...!)
-
osrl about 8 yearsYou should use swift style dictionary.
[NSFontAttributeName: font]
-
itzmebibin over 7 yearsobjFont returns
nil
value. -
vahotm about 7 yearsThis 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 over 6 yearsPlease don't scale standard controls.
-
L_J almost 6 yearsWhile this code may answer the question, providing information on how and why it solves the problem improves its long-term value
-
Apoorv Khatreja over 5 yearsThis 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 over 5 years-- update -- NSDictionary *attributes = @{NSFontAttributeName:font}; [mySegmentedcontrol setTitleTextAttributes:attributes forState:UIControlStateNormal];
-
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 about 5 yearsYou can drop the key down to simply
.font
as well -
Coding while Loading almost 5 yearsYou actually have to call it like this let font = UIFont.systemFont(ofSize: 16) UISegmentedControl.appearance().setTitleTextAttributes([NSAttributedString.Key.font: font], for: .normal)
-
Sky over 4 yearsError :
Instance member 'setTitleTextAttributes' cannot be used on type 'UISegmentedControl'; did you mean to use a value of this type instead?
iOS13/Swift5 -
Fattie over 2 yearsthis answer is wrong in 2021