1

I added a custom font called "Quicksand_Dash.otf", "Quicksand-Bold.otf" to an iOS project. I can see it in Interface builder but can't see when I list fonts in code.

  • "Fonts provided by application" and relevant name added to plist file.

  • I checked If font file is in copy bundle resources.

Copy Bundle Resources

Ss 2

ss 3

How I list the font;

for family in UIFont.familyNames {

        let sName: String = family as String
        print("family: \(sName)")

        for name in UIFont.fontNames(forFamilyName: sName) {
            print("name: \(name as String)")
        }
 }

I checked probably every answer in stack overflow but I think I have a different problem.

How To Use Custom Fonts in iPhone SDK

Custom Fonts Not Working

EDIT: If I add a label in storyboard and make its font Quicksand. Font is also listed in code. However, I don't wanna user storyboard. I'm creating controllers in code.

EDIT2: If I delete label in storyboard. It started to not seen again.

This is the same problem I'm facing but I applied the solutions in that post already;

Custom Font only available in Interface Builder

Emre Önder
  • 2,408
  • 2
  • 23
  • 73

4 Answers4

3

You're going to kick yourself...

In your plist info file, you have:

`Quicksand-Dash.otf`

but the file you added is named:

`Quicksand_Dash.otf`

underscore not hyphen

Either rename your file, or edit the plist entry, and you'll see your font.

Debug console output:

family: Quicksand
name: Quicksand-BoldItalic
name: QuicksandDash-Regular
name: Quicksand-Bold

Here is a plain single-view project that works fine for me - on both Simulator and Device. (I have not added the fonts to OS X, only to my project):

https://github.com/DonMag/Quicksand

Just for setup reference:

enter image description here

enter image description here

DonMag
  • 69,424
  • 5
  • 50
  • 86
  • Sorry I forgot to edit screenshot of plist file. Actually, I fixed that but problem is still continue. – Emre Önder Apr 09 '18 at 12:27
  • Hmmm... I downloaded the fonts (from some site, they appear to be available many places)... I dragged them into the project... made sure Target Membership was selected... add the names to the plist... and only `-Bold` and `-BoldItalic` showed up. I changed the name of the file to `-` instead of `_`, re-ran (on device), and that's the output I got. Try again with a fresh project - must be something else going on. – DonMag Apr 09 '18 at 12:34
  • Or are you saying it **does** show up in the font list output, but you can't get it to display in a label? – DonMag Apr 09 '18 at 12:35
  • Maybe this is the problem with xcode it takes some time to add it in project because I have used custom fonts in two another project before and they are showing – Wings Apr 09 '18 at 12:38
  • Actually I don't understand why they list in code when I add in IB but not If I don't use it in storyboard. – Emre Önder Apr 09 '18 at 12:39
  • I think the same and I follow the same procedure but it will not showing the fonts and I don't think so all the code available on internet is wrong – Wings Apr 09 '18 at 12:42
  • @EmreÖnder - Try the GitHub project I just posted: https://github.com/DonMag/Quicksand --- no label in IB, all from code. – DonMag Apr 09 '18 at 12:50
  • Can you please also check my answer. – Emre Önder Apr 09 '18 at 13:03
  • Can you explain it Why you use Guard Statement because I used too but I created the extension of Fonts and it is showing in console now – Wings Apr 09 '18 at 13:06
  • Don't add another answer if this solved your issue. If you open your info plist file as "Source Code" you will see that all of the Keys are different than the "Readable" labels. So, `UIAppFonts` is what you'll find in the plist file, and what you'll use if you manually edit that file, but when you close and re-open the project is *should* revert to `Fonts provided by application` – DonMag Apr 09 '18 at 13:08
  • As with all Swift Optionals, you want to either use `if let ...` or `guard let ...` to make sure you handle any error conditions. It has no effect on the fonts showing up, if they were added and defined correctly. – DonMag Apr 09 '18 at 13:09
  • Okk.... but I don't think so this is the right answer because after adding extension code to my project. Console is showing the fonts and I tried many times before to add but that doesn't show – Wings Apr 09 '18 at 13:15
  • @V_rohit - no, as I said, *"It has no effect on the fonts showing up"* ... nor would it have anything to do with adding any *extension code*. – DonMag Apr 09 '18 at 13:16
  • @EmreÖnder - is it possible you had typed the full key name of `Fonts provided by application` but added a trailing space maybe? In that case, it would ***not*** be the correct key. – DonMag Apr 09 '18 at 13:20
  • when I open it with source code it is showing UIAppFonts and in plist it shows FontsProvidedByApplication – Wings Apr 09 '18 at 13:21
  • If that was the problem then it doesn't print in console but it is printing the fonts – Wings Apr 09 '18 at 13:25
3

I also stuck to this issue and given solutions don't work for me. In my case ttf file's "Target Membership" block is uncheck. So when I click on it, the respected ttf font listed in font family list.image attach for reference

Happy Coding!!

bittu
  • 683
  • 7
  • 24
0

From reference of @Donmag answer. I download his example project from GitHub and copy fonts from his project to my project then copy "Fonts provided by application" area from plist to my plist. Interestingly, It copied as "UIAppFonts". However, It started to work now.

SS

Emre Önder
  • 2,408
  • 2
  • 23
  • 73
-1

To use custom fonts in application

  • Add fonts to your project folder
  • add it in plist file
  • check if its added in copy bundle resource
  • add font to your font book

Check the below link for more reference.

https://stackoverflow.com/a/28843547/9332509

Krishna Maru
  • 164
  • 13