27

Is there a way to have custom fonts to the title of a button.

xydev
  • 3,409
  • 5
  • 34
  • 54

4 Answers4

50

The UIButton class exposes a titleLabel property, upon which you can change things like the font, the text color, etc.

zed_0xff
  • 32,417
  • 7
  • 53
  • 72
Justin Spahr-Summers
  • 16,893
  • 2
  • 61
  • 79
  • 12
    Note some properties of `titleLabel` are reset after `setTitle()`. – cvsguimaraes Feb 03 '13 at 14:15
  • 3
    Its not recommended to set textColor property of the titleLable according to Apple's documentation "Do not use the label object to set the text color or the shadow color. Instead, use the setTitleColor:forState: and setTitleShadowColor:forState: methods of this class to make those changes." https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIButton_Class/#//apple_ref/occ/instp/UIButton/titleLabel – Omaty Apr 30 '15 at 12:10
22

Set the font of the button's titleLabel:

myButton.titleLabel.font = [UIFont fontWithName:@"Courier" size:22.0];
Darren
  • 25,520
  • 5
  • 61
  • 71
2

In case you want to list all available fonts and then select your font, run this Swift code:

UIFont.familyNames().forEach {
    print($0)
    UIFont.fontNamesForFamilyName($0).forEach {
        print("  \($0)")
    }
}

It prints all font names for their font families. Then assign the same font name as printed.

button.titleLabel.font = UIFont(name: "OpenSans", size: 20.0)
Lars Blumberg
  • 19,326
  • 11
  • 90
  • 127
2

You can always render text in a UIImage and then configure the button to use that with setImage:forState:. This allows you to do custom fonts, labels with icons, images, etc.

Stefan Arentz
  • 34,311
  • 8
  • 67
  • 88