How do you stylize a font in Swift?

user3763693 picture user3763693 · Jul 12, 2014 · Viewed 109.9k times · Source

I'm trying out developing for Swift, it's going pretty well. One of the issues I'm having is finding out how to stylize fonts programmatically in the language.

For example in this label I wrote the code below for, how can I make it Helvetica Neue Ultralight for example?

label.font = UIFont (name: "Helvetica Neue", size: 30)

Answer

KRUKUSA picture KRUKUSA · Jul 12, 2014

I am assuming this is a custom font. For any custom font this is what you do.

  1. 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”).

  2. In your Info.plist file add the key “Fonts provided by application” with type “Array”.

  3. 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. HelveticaNeue-UltraLight.ttf). Save your “Info.plist” file.

label.font = UIFont (name: "HelveticaNeue-UltraLight", size: 30)