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)
To change the font or the size of a UILabel in a Storyboard or . XIB file, open it in the interface builder. Select the label and then open up the Attribute Inspector (CMD + Option + 5). Select the button on the font box and then you can change your text size or font.
I am assuming this is a custom font. For any custom font this is what you do.
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. HelveticaNeue-UltraLight.ttf). Save your “Info.plist” file.
label.font = UIFont (name: "HelveticaNeue-UltraLight", size: 30)
A great resource is iosfonts.com, which says that the name for that font is HelveticaNeue-UltraLight
. So you'd use this code:
label.font = UIFont(name: "HelveticaNeue-UltraLight", size: 30)
If the system can't find the font, it defaults to a 'normal' font - I think it's something like 11-point Helvetica. This can be quite confusing, always check your font names.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With