42
votes

How to change font size and font name of uisegmentedcontrol programmatically? I used swift.

Here is my code:

self.mysegmentedControl = UISegmentedControl(items: [
        NSLocalizedString("Aaaaaa", comment: ""),
        NSLocalizedString("Bbbbbb", comment: ""),
        NSLocalizedString("Cccccc", comment: ""),
        NSLocalizedString("Dddddd", comment: ""),
        NSLocalizedString("Eeeeee", comment: ""),
        ])
self.mysegmentedControl.addTarget(self, action: "mysegmentedControlDidChange:", forControlEvents: .ValueChanged)
self.mysegmentedControl.selectedSegmentIndex = 0

regards.

15

15 Answers

80
votes

UI can use control appearance, best place to add it is in app delegate, didFinishLaunchingWithOptions method, use this if you want to set up the same attribute to every UISegmentedControls in your project just once:

let attr = NSDictionary(object: UIFont(name: "HelveticaNeue-Bold", size: 16.0)!, forKey: NSFontAttributeName)
UISegmentedControl.appearance().setTitleTextAttributes(attr as [NSObject : AnyObject] , forState: .Normal)

But if you are going to set up attributes to just one UISegmentedControl or if you want to change it more often base on some condition use this, UISegmentedControl method:

func setTitleTextAttributes(_ attributes: [NSObject : AnyObject]?,
                   forState state: UIControlState)

Example:

let attr = NSDictionary(object: UIFont(name: "HelveticaNeue-Bold", size: 16.0)!, forKey: NSFontAttributeName)
seg.setTitleTextAttributes(attr as [NSObject : AnyObject] , forState: .Normal)
27
votes

For Swift 4

    let font: [AnyHashable : Any] = [NSAttributedStringKey.font : UIFont.systemFont(ofSize: 17)]
    segmentedControl.setTitleTextAttributes(font, for: .normal)
21
votes

This answer is dated, but for those who are looking for a solution in swift you might want to try this approach:

func stylizeFonts(){
    let normalFont = UIFont(name: "Helvetica", size: 16.0)
    let boldFont = UIFont(name: "Helvetica-Bold", size: 16.0)

    let normalTextAttributes: [NSObject : AnyObject] = [
        NSForegroundColorAttributeName: UIColor.blackColor(),
        NSFontAttributeName: normalFont!
    ]

    let boldTextAttributes: [NSObject : AnyObject] = [
        NSForegroundColorAttributeName : UIColor.whiteColor(),
        NSFontAttributeName : boldFont!,
    ]

    self.setTitleTextAttributes(normalTextAttributes, forState: .Normal)
    self.setTitleTextAttributes(normalTextAttributes, forState: .Highlighted)
    self.setTitleTextAttributes(boldTextAttributes, forState: .Selected)
}

Be sure to add stylizeFonts() in your viewDidLoad or as a separate function if you are subclassing.

11
votes

Greg's answer updated for Swift3:

let attr = NSDictionary(object: UIFont(name: "OpenSans", size: 12.0)!, forKey: NSFontAttributeName as NSCopying)
UISegmentedControl.appearance().setTitleTextAttributes(attr as [NSObject : AnyObject] , for: .normal)
7
votes

Swift 2.0

    override func viewDidLayoutSubviews() {
        let attributedSegmentFont = NSDictionary(object: UIFont(name: "Roboto-Regular", size: 14.0)!, forKey: NSFontAttributeName)

    dashBoardSegment.setTitleTextAttributes(attributedSegmentFont as [NSObject : AnyObject], forState: .Normal)
    }

Changing for all segment controls, use:

UISegmentedControl.appearance().setTitleTextAttributes(attributedSegmentFont as [NSObject : AnyObject], forState: .Normal)

Using Swift Extension:

extension UISegmentedControl{
    func changeTitleFont(newFontName:String?, newFontSize:CGFloat?){
        let attributedSegmentFont = NSDictionary(object: UIFont(name: newFontName!, size: newFontSize!)!, forKey: NSFontAttributeName)
        setTitleTextAttributes(attributedSegmentFont as [NSObject : AnyObject], forState: .Normal)
    }
}

Implementing extension:

override func viewDidLayoutSubviews() {
    dashBoardSegment.changeTitleFont("Roboto-Regular", newFontSize: 14.0)
}
6
votes

Swift3

override func viewDidLayoutSubviews() {
    let attr = NSDictionary(object: UIFont(name: "Chalkduster", size: 14.0)!, forKey: NSFontAttributeName as NSCopying)
    segmentedControl.setTitleTextAttributes(attr as [NSObject : AnyObject] , for: .normal)
}
4
votes

By way of extending mvien's excellent Swift approach. I've created a SegmentedControl extension:

extension UISegmentedControl {

    func setFontSize(fontSize: CGFloat) {

        let normalTextAttributes: [NSObject : AnyObject] = [
            NSForegroundColorAttributeName: UIColor.blackColor(),
            NSFontAttributeName: UIFont.systemFontOfSize(fontSize, weight: UIFontWeightRegular)
        ]

        let boldTextAttributes: [NSObject : AnyObject] = [
            NSForegroundColorAttributeName : UIColor.whiteColor(),
            NSFontAttributeName : UIFont.systemFontOfSize(fontSize, weight: UIFontWeightMedium),
        ]

        self.setTitleTextAttributes(normalTextAttributes, forState: .Normal)
        self.setTitleTextAttributes(normalTextAttributes, forState: .Highlighted)
        self.setTitleTextAttributes(boldTextAttributes, forState: .Selected)
    }
}

Just add the above extension code to your project then use like this:

yourSegmentedControl.setFontSize(20)
4
votes

@Jordan Montel answer updated for Swift 5. Working code to change font and font size.

extension UISegmentedControl {
    func font(name:String?, size:CGFloat?) {
        let attributedSegmentFont = NSDictionary(object: UIFont(name: name!, size: size!)!, forKey: NSAttributedString.Key.font as NSCopying)
        setTitleTextAttributes(attributedSegmentFont as [NSObject : AnyObject] as [NSObject : AnyObject] as? [NSAttributedString.Key : Any], for: .normal)
    }
}

usage "segControl" is what ever your IBOutlet name is:

segControl.font(name: "Your Font Name", size: 10)
3
votes

For Swift 4

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

Swift 4

@IBOutlet var sgmentTypeSelect: UISegmentedControl!{
        didSet {

            let normalTextAttributes: [NSAttributedStringKey : AnyObject] = [
                NSAttributedStringKey.foregroundColor : UIColor.themeGold,
                NSAttributedStringKey.font : UIFont.defaultMontserratFont(style: "SemiBold", size: 13)
            ]

            let selectedTextAttributes: [NSAttributedStringKey : AnyObject] = [
                NSAttributedStringKey.foregroundColor : UIColor.black,
                NSAttributedStringKey.font : UIFont.defaultMontserratFont(style: "Bold", size: 13)
            ]

            sgmentTypeSelect.setTitleTextAttributes(normalTextAttributes, for: .normal)
            sgmentTypeSelect.setTitleTextAttributes(normalTextAttributes, for: .highlighted)
            sgmentTypeSelect.setTitleTextAttributes(selectedTextAttributes, for: .selected)

        }
    }
1
votes

Using @Alvin George answer because I think it is a great idea to add extension for manipulate that (and improve it for Swift 4) :

Create an extension class of UISegmentedControl like UISegmentedControl+Additions.swift

import Foundation
import UIKit

extension UISegmentedControl {
    func font(name:String?, size:CGFloat?) {
        let attributedSegmentFont = NSDictionary(object: UIFont(name: name!, size: size!)!, forKey: NSAttributedStringKey.font as NSCopying)
        setTitleTextAttributes(attributedSegmentFont as [NSObject : AnyObject], for: .normal)
    }
}

Use it in your code :

segmentedControl?.font(name: "My Font Name", size: 12)
1
votes

Xamarin/C# Solution

Here's the breakdown for changing with UIAppearance based on the approved answer:

var font = UIFont.FromName("HelveticaNeue-Bold", 16f);
var textAttributes = new UITextAttributes { Font = font };
UISegmentedControl.Appearance.SetTitleTextAttributes(textAttributes, 
    UIControlState.Normal);
1
votes

Further to jeff-ziligy answer I have played around with the code and found an updated code for Swift 5.1:

extension UISegmentedControl {

func setFontSize(fontSize: CGFloat) {

    let normalTextAttributes: [NSObject : AnyObject] = [
        NSAttributedString.Key.foregroundColor as NSObject: UIColor.black,
        NSAttributedString.Key.font as NSObject : UIFont.systemFont(ofSize: fontSize, weight: UIFont.Weight.medium)
    ]

    let boldTextAttributes: [NSObject : AnyObject] = [
        NSAttributedString.Key.foregroundColor as NSObject : UIColor.black,
        NSAttributedString.Key.font as NSObject : UIFont.systemFont(ofSize: fontSize, weight: UIFont.Weight.medium),
    ]

    self.setTitleTextAttributes(normalTextAttributes as? [NSAttributedString.Key : Any], for: .normal)
    self.setTitleTextAttributes(normalTextAttributes as? [NSAttributedString.Key : Any], for: .highlighted)
    self.setTitleTextAttributes(boldTextAttributes as? [NSAttributedString.Key : Any], for: .selected)
  }
}

The part where up implement in the ViewDidLoad() is the same:

yourSegmentedControl.setFontSize(20)
0
votes

The Objective-C version:

NSDictionary *attributes = @{ NSFontAttributeName : [UIFont fontWithName:@"HelveticaNeue-Light" size:17] };
[segmentedControl setTitleTextAttributes:attributes forState:UIControlStateNormal];
-2
votes

For swift 3

UISegmentedControl.appearance().setTitleTextAttributes([NSForegroundColorAttributeName:UIColor.white], for: .selected)