custom font not working programmatically in swift

Let’s say you want to add this font: SourceSansPro-Regular.otf

Four steps:

  1. Add the font file SourceSansPro-Regular.otf to your project, make sure you select your target in the “Add to targets”.

enter image description here

Go to the target’s Build Phases and make sure it is under Copy Bundle Resources. If not, add it.

enter image description here

2. Go to the target’s Info. Add a new entry Font provided by application then add a new item with this value SourceSansPro-Regular.otf.

enter image description here

  1. From your finder, double click on the file SourceSansPro-Regular.otf, it might ask you to install the font to your Font Book.
  1. Open the OS X Font Book application, navigate to your font then press Command+i. Note the PostScript name and use that name in your Swift code. In this case, it’s SourceSansPro-Regular.
    enter image description here

So in your code:

labeladd.font = UIFont(name:"SourceSansPro-Regular", size:15)

Leave a Comment