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)
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.
You can set custom font in two ways : design time
and run-time
.
First you need to download required font
(.ttf file format). Then, double click on the file to install it.
This will show a pop-up. Click on 'install font
' button.
'Fonts'
window.Add to targets
' is checked.Copy Bundle Resources'
present in Build Phases
of Targets
of your project.Info.plist
of your project.
Create a new key with 'Font Provided by application
' with type as Array.
Add a the font as an element with type String in Array.A. Design mode :
Font
attribute present in Attribute inspector
of Utilities
.B. Run-time mode :
self.lblWelcome.font = UIFont(name: "BananaYeti-Extrabold Trial", size: 16)
It seems that run-time mode doesn't work for dynamically formed string like
self.lblWelcome.text = "Welcome, " + fullname + "!"
Note that in above case only design-time approach worked correctly for me.
Add Custom Font in Swift
myLabel.font = UIFont (name: "GILLSANSCE-ROMAN", size: 20)
Xamarin
Label.Font = UIFont.FromName("Copperplate", 10.0f);
Swift
text.font = UIFont.init(name: "Poppins-Regular", size: 14)
To get the list of font family Github/IOS-UIFont-Names
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)
Source: Stackoverflow.com