4

I am trying to change the font of a button on my view controller in my code because it is not working in the storyboard. For some reason, I keep trying but am unable to change the font of my button. Here's what my code looks like. The font name I chose is "Print Clearly" and I already set it up correctly so that it works on text fields, but am having trouble with buttons.

@IBOutlet weak var songSuggestButton: UIButton!
  var theFont : UIFont = UIFont(name: "Print Clearly", size: 12)!

override func viewDidLoad() {
  super.viewDidLoad()
  songSuggestButton.titleLabel?.font = theFont
  print(songSuggestButton.titleLabel?.font.fontDescriptor())
}

The print tells me the font is Print Clearly and size 12, but still shows up as the default font when I run my app.

Update: The second answer actually worked for me.

> Try this:

@IBOutlet weak var songSuggestButton: UIButton!

override func viewDidAppear() {
 super.viewDidAppear(false)
 songSuggestButton.titleLabel?.font = UIFont(name: "Print Clearly", size: 12)!
 print(songSuggestButton.titleLabel?.font.fontDescriptor())
}

Strange how my custom fonts for buttons only work when initiated through this code. I also wanted to have different sizes of the font for my different view sizes, is there a way about doing this? Thanks.

Daniel Doubt
  • 49
  • 1
  • 1
  • 6
  • 2
    [are you sure about the font name ?](http://stackoverflow.com/questions/31420758/swift-custom-fonts-xcode-7-0-beta/31420849#31420849) – Alaeddine May 11 '16 at 18:24
  • @DanielDoubt For : "I also wanted to have different sizes of the font for my different view sizes, is there a way about doing this? Thanks." That's an entirely different question. So please ask another one :D – Coder1000 May 12 '16 at 10:30

1 Answers1

11

You can follow these instructions:

  • First download and add your font files to your project in Xcode (The files should appear as well in “Target -> Build Phases -> Copy Bundle Resources”).
  • In your Info.plist file add the key “Fonts provided by application” with type “Array”.
  • For each font you want to add to your project, create an item for the array you have created with the full name of the file including its extension (e.g. PrintClearly.ttf). Save your “Info.plist” file.
  • Do not forget to include your custom fonts in the "Copy Bundle Resources" section if you haven't already or you won't be able to use them

Now you can use the following code:

songSuggestButton.titleLabel?.font = UIFont (name: "Print Clearly", size: 12)

UPDATE:

You can put this code both in viewDidLoad or viewDidAppear, UIKit is not thread safe and should only be updated from the main thread.

So to make sure you will update your UI on the main thread you can do:

dispatch_async(dispatch_get_main_queue()) {
    songSuggestButton.titleLabel?.font = theFont
    songSuggestButton.setNeedsDisplay()
}

To answer to your last question you must know that label is constrained, and when you change the label size, you change the label frame dimension and so change also his constraints. If you have few view, the best way in iOS is to make different xib's with different label size and load the best xib for that view.

Alessandro Ornano
  • 34,887
  • 11
  • 106
  • 133