How to change UIButton font with Swift

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

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

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

Any suggestions?

+135
ios swift uibutton uifont
Jul 28 '14 at 18:39
source share
16 answers

titleLabel this use titleLabel . The font property is deprecated in iOS 3.0. This also does not work in Objective-C. titleLabel is the label used to display the title on the UIButton .

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

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

+295
Jul 28 '14 at 18:42
source share

For Swift 3.0 :

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

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

+52
Jan 06 '17 at 11:30
source share

The dot notation is awesome (swift 4.2) 👌

 btn.titleLabel?.font = .systemFont(ofSize: 12) 
+12
May 16 '18 at 12:03
source share

You do not need to force the titleLabel to deploy to set it.

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

Since you are not using titleLabel here, you can just use it, and if it is zero, it will be just no-op.

I will also add, as other people say, the font property is deprecated and remember to use setTitle:forControlState: when setting the title text.

+7
Mar 11 '16 at 19:34
source share

From the documentation :

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

+6
Jul 28 '14 at 18:42
source share

If you have problems with the font size (your font does not respond to resizing) ...

@codester has the correct code:

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

However, my font size did not change. Turns out I'm asking for a font that wasn't there ("HelveticaNeue-Regular"). It didn't crash, but it seems to just ignore this font statement because of this. As soon as I changed the font to what exists, the changes in "size: x" were displayed.

+5
Apr 19 '16 at 12:50
source share

Take a look here .

Instead, you should set the font for the titleLabel button.

 myButton.titleLabel!.font = UIFont(name: "...", 10) 
+4
Jul 28 '14 at 18:42
source share

This works in Swift 3.0:

 btn.titleLabel?.font = UIFont(name:"Times New Roman", size: 20) 
+4
Aug 21 '17 at 7:15
source share

You must go through the titleLabel property.

button.titleLabel.font

The font property has been deprecated since iOS 3.0.

+3
Jul 28 '14 at 18:43
source share

If you set the AttributedString to UIButton, then you can do the following.

 let attributedText = NSAttributedString(string: "Hello", attributes: [NSAttributedStringKey.font: UIFont(name: "Calibri", size: 19)]) okayButton.setAttributedTitle(attributedText, for: .normal) 
+3
Apr 26 '18 at 5:42
source share

If you only need to resize ( Swift 4.0 ):

 button.titleLabel?.font = button.titleLabel?.font.withSize(12) 
+2
Sep 16 '18 at
source share

we can use various types of system fonts as shown 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 own font as below

  myButton.titleLabel?.font = UIFont(name: "Helvetica", size:12) 
+2
Jan 01 '19 at 7:40
source share

Now this method does not work:

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

It works:

  btn.titleLabel?.font = UIFont.init(name: "Helvetica", size:12) 
+1
Jan 25 '17 at 15:38
source share

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

  • If you want to use the font defaul from your own family, use, for example: "HelveticaNeue"
  • If you want to specify a family font, use, for example: "HelveticaNeue-Bold"
+1
Nov 19 '17 at 12:03 on
source share

this work is for me, thanks. I want to change only the size of the text, and not change the font name.

 var fontSizeButtonBig:Int = 30 

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

0
Aug 14 '18 at 2:51
source share

In Xamarin.iOS, you can write C # code as follows:

myButton.TitleLabel.Font = UIFont.FromName ("Helvetica-Bold", 33f);

-one
Oct 18 '16 at 2:55
source share



All Articles