Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you stylize a font in Swift?

Tags:

ios

swift

uifont

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) 
like image 832
user3763693 Avatar asked Jul 12 '14 00:07

user3763693


People also ask

How do I change the font style in Swift?

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.


2 Answers

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)

like image 79
KRUKUSA Avatar answered Oct 21 '22 14:10

KRUKUSA


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.

like image 38
Undo Avatar answered Oct 21 '22 14:10

Undo