283

I am trying to change the font of a UIButton using Swift...

myButton.font = UIFont(name: "...", 10)

However .font is deprecated and I'm not sure how to change the font otherwise.

Any suggestions?

rmaddy
  • 314,917
  • 42
  • 532
  • 579
Stephen Fox
  • 14,190
  • 19
  • 48
  • 52

19 Answers19

550

Use titleLabel instead. The font property is deprecated in iOS 3.0. It also does not work in Objective-C. titleLabel is label used for showing title on UIButton.

myButton.titleLabel?.font =  UIFont(name: YourfontName, size: 20)

However, while setting title text you should only use setTitle:forControlState:. Do not use titleLabel to set any text for title directly.

gi097
  • 7,313
  • 3
  • 27
  • 49
codester
  • 36,891
  • 10
  • 74
  • 72
  • 1
    Do you guys know why I have to 'unwrap'? my label?? like this... myButton.titleLabel!.font = ... – DanMoore Sep 13 '14 at 02:18
  • beacause `titleLabel` is optional property.Thanks previously in beta it was not. – codester Sep 13 '14 at 03:58
  • 7
    This method allowed me to change my font name/style, but changing the size property made no difference. – Dave G Aug 24 '15 at 17:17
  • 7
    You don't need to force unwrap the label. `myButton.titleLabel?.font = ...` will work since if titleLabel is nil it will just be a no-op – teradyl Mar 11 '16 at 19:37
  • 3
    Why doe this method not work for custom UIbutton subclass? – Munib Jun 05 '19 at 21:17
  • I had some trouble finding my "YourfontName" specifically, until I found this list of all the names: https://gist.github.com/tadija/cb4ec0cbf0a89886d488d1d8b595d0e9 . There's ways to get them spit out programmatically, but I've lost that code since I implemented it a few years ago, if I find it I will post it here (used it to grab a custom font name). – agrippa Mar 31 '20 at 10:19
  • @Munib I have. How did you solve that? – Mahdi Moqadasi Oct 02 '21 at 11:19
89

As mentioned by many here you can set the font with something like:

button.titleLabel?.font = .systemFont(ofSize: 19.0, weight: .bold)

Just make sure your button has default style though for this to be applicable, otherwise the above gets ignored by the system.

enter image description here

atineoSE
  • 3,597
  • 4
  • 27
  • 31
77

For Swift 3.0:

button.titleLabel?.font = UIFont.boldSystemFont(ofSize: 16)

where "boldSystemFont" and "16" can be replaced with your custom font and size.

ruthless_g
  • 871
  • 7
  • 4
73

Dot-notation is awesome

btn.titleLabel?.font = .systemFont(ofSize: 12)
Sentry.co
  • 5,355
  • 43
  • 38
  • @mahasam UIFont is inferred, this answer is the same than the others but just without UIFont (but becasuse it is inferred by the compiler, but it is the same) P.D: I always try to write in this way, but just to let you know that is the same – Pablo Sanchez Gomez Oct 19 '18 at 10:39
  • 3
    @mahasam: "Dot-notation" is the same as inferred. And yes It's a good habit to adopt. And it's not the same answers as others as its more succinct than other answers. IMO – Sentry.co Oct 19 '18 at 10:50
11

If you need to change only size (Swift 4.0):

button.titleLabel?.font = button.titleLabel?.font.withSize(12)
Petr Lazarev
  • 3,102
  • 1
  • 21
  • 20
11

In Swift 5, you can utilize dot notation for a bit quicker syntax:

myButton.titleLabel?.font = .systemFont(ofSize: 14, weight: .medium)

Otherwise, you'll use:

myButton.titleLabel?.font = UIFont.systemFont(ofSize: 14, weight: .medium)

andrewlundy
  • 1,073
  • 15
  • 22
10

You don't need to force unwrap the titleLabel to set it.

myButton.titleLabel?.font = UIFont(name: YourfontName, size: 20)

Since you're not using the titleLabel here, you can just optionally use it and if it's nil it will just be a no-op.

I'll also add as other people are saying, the font property is deprecated, and make sure to use setTitle:forControlState: when setting the title text.

teradyl
  • 2,584
  • 1
  • 25
  • 34
9

If you are setting AttributedString to the UIButton then you can do the below thing.

let attributedText = NSAttributedString(string: "Hello", attributes: [NSAttributedStringKey.font: UIFont(name: "Calibri", size: 19)])
okayButton.setAttributedTitle(attributedText, for: .normal)
Anirudha Mahale
  • 2,526
  • 3
  • 37
  • 57
8

From the documentation:

The font used to display text on the button. (Deprecated in iOS 3.0. Use the font property of the titleLabel instead.)

zneak
  • 134,922
  • 42
  • 253
  • 328
7

If you're having font size issues (your font isn't responding to size changes)...

@codester has the right code:

myButton.titleLabel!.font =  UIFont(name: YourfontName, size: 20)

However, my font size wasn't changing. It turns out that I asking for a font that didn't exist ("HelveticaNeue-Regular"). It wasn't causing a crash, but seemed to be just ignoring that font statement because of it. Once I changed the font to something that does exist, changes to "size: x" did render.

Dave G
  • 12,042
  • 7
  • 57
  • 83
7

we can use different types of system fonts like below

myButton.titleLabel?.font = UIFont.boldSystemFont(ofSize: 17)
myButton.titleLabel?.font = UIFont.italicSystemFont(ofSize:UIFont.smallSystemFontSize)
myButton.titleLabel?.font = UIFont.boldSystemFont(ofSize: UIFont.buttonFontSize)

and your custom font like below

    myButton.titleLabel?.font = UIFont(name: "Helvetica", size:12)
Sultan Ali
  • 2,497
  • 28
  • 25
6

Take a look here.

You should set the font of the button's titleLabel instead.

myButton.titleLabel!.font = UIFont(name: "...", 10)
Community
  • 1
  • 1
Connor Pearson
  • 63,902
  • 28
  • 145
  • 142
5

You should go through the titleLabel property.

button.titleLabel.font

The font property has been deprecated since iOS 3.0.

Chris Wagner
  • 20,773
  • 8
  • 74
  • 95
5

This works in Swift 3.0:

btn.titleLabel?.font = UIFont(name:"Times New Roman", size: 20) 
Bugs
  • 4,491
  • 9
  • 32
  • 41
ram
  • 69
  • 1
  • 2
3

Example: button.titleLabel?.font = UIFont(name: "HelveticaNeue-Bold", size: 12)

  • If you want to use defaul font from it's own family, use for example: "HelveticaNeue"
  • If you want to specify family font, use for example: "HelveticaNeue-Bold"
janaz
  • 673
  • 5
  • 12
2

This way doesn't work now:

 btn.titleLabel?.font = UIFont(name: "Helvetica", size:12)

This works:

 btn.titleLabel?.font = UIFont.init(name: "Helvetica", size:12)
RealNmae
  • 630
  • 9
  • 20
2

To do this using storyboard, go to the attributes inspector while your button is selected. In the third field from the top ("Title") select "Attributed". This will bring up the font drop-down list where you can easily change the font.

23shay83
  • 31
  • 3
1

Swift 5

myButton.titleLabel?.font = UIFont(name: "yourCustomFont", size: CGFloat(yourFontSize))
Adriaan
  • 17,741
  • 7
  • 42
  • 75
  • 1
    Please read [answer] and [edit] your answer to contain an explanation as to why this code would actually solve the problem at hand. Always remember that you're not only solving the problem, but are also educating the OP and any future readers of this post. – Adriaan Jan 31 '23 at 12:57
  • While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - [From Review](/review/late-answers/33746963) – storoj Feb 05 '23 at 01:51
0

this work for me, thanks. I want change text size only not change font name.

var fontSizeButtonBig:Int = 30

btnMenu9.titleLabel?.font = .systemFont(ofSize: CGFloat(fontSizeButtonBig))

mahasam
  • 829
  • 7
  • 8