I am trying to use custom font (Bebas Neue) in my iOS application. The steps I took are:
- Copy the .otf files to the project.
- Confirm the .otf files have set the project as target.
- Added the .otf files in 'Fonts provided by application' in plist.
- In Build Phases, the .otf files are in 'Copy Bundle Resources'.
- Install the font on my Mac.
- Try to print out all fonts available but I can't see my custom font.
The code I used:
for name in UIFont.familyNames() {
println(name)
if let nameString = name as? String
{
println(UIFont.fontNamesForFamilyName(nameString))
}
}
- Trying to set the font in code and it worked.
The code I used:
textLabel?.font = UIFont(name: "BebasNeueRegular", size: 14)
- But I can't set it in interface builder. Any idea?
Screenshots:
Best Answer
Try Below Steps: Code tested in
Swift 3
.Step 1: Add Your custom font into your project( Make sure Add to Target ticked).I am using "PermanentMarker.ttf","Pecita.otf" and "AROLY.ttf" font as a test font.
Note: Supporting font Type ttf and otf (Both font types should work)
Step 2: Modify the application-info.plist file. Add the key "Fonts provided by application" in a new row and add "PermanentMarker.ttf" as new item in the Array "Fonts provided by application".
Your plist should looks like this
Now the font will be available in Interface Builder. To use the custom font in code we need to refer to it by name, but the name often isn’t the same as the font’s filename
Now, You can access the Custom Font from your viewController. I am testing the font by placing a UIlabel to the Storyboard like below.
Update 2:
Working Solution
After, imported your custom font and updated your
plist
.selectlabel
from yourstoryBoard
,gotoAttributes Inspector
underLabel
>Text type
> select toAttributed
and choose yourcustom font
from the list.Output:
Update 1
If your custom font still not listed in
Xcode font list
.check the related link to your issuehttp://codewithchris.com/common-mistakes-with-adding-custom-fonts-to-your-ios-app/
custom font not displaying on some simulator
Note: Still,You can assign
BebasNeue
orcustom font
programatically to your label or button etc. even its not showing in yourinterface Builder
.If you having trouble setting font to your object programatically.try below method.Assign font to UILabel:
Assign font to UIButton:
Assign font to UITextField:
Assign font to UINavigationBar:
Assign font to UIToolBar:
Output: