简体   繁体   中英

Custom font seen in Interface builder but not in code in iOS project

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.

复制捆绑包资源

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

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:

在此处输入图片说明

在此处输入图片说明

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. 图片附件供参考

Happy Coding!!

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM