Custom Fonts only available when set in Interface Builder

前端 未结 6 1084
暖寄归人
暖寄归人 2021-01-13 08:50

I have added a custom font to my project. It is included in the target, and it is added in the plist. When I try to use it programmatically it doesn\'t work, and it doesn\'t

相关标签:
6条回答
  • 2021-01-13 09:22

    I know this is old but I just ran into the same issue. The font would show up only if I selected that font in the interface builder. Turns out when I searched for UIAppFonts, it didn't list the main Info.plist (we were already using custom fonts) for some reason. So I ended up adding the fonts to the localized (?) plist file which resulted in this issue. What I had to do was to go to the Project Settings > Target > Info tab and add the font file names there.

    0 讨论(0)
  • 2021-01-13 09:32

    Ok, topic is old but I would like to put in my two cents here.

    First off all, read this article http://codewithchris.com/common-mistakes-with-adding-custom-fonts-to-your-ios-app/

    In brief:

    1. remember to include your fonts to project
    2. setup target for fonts
    3. check if your fonts are included as resources in bundle
    4. add custom fonts to plist file
    5. check the real name of your font in code

    When I did everything like described in this article, my fonts works in interface builder only. I also have problem with step 5 - my fonts did not show up in console output.

    Ok, so now is time for tips from me:

    TIP #1

    use otf font format, if you have different, convert it (or ask your designer ;) ) you can use this site for that: https://onlinefontconverter.com/

    TIP #2

    In article above you can find:

    Open it and add a new row called “Fonts provided by application” which will be an array that you need to add all the filenames of the fonts you want to use.

    Instead of "Fonts provided by application" enter "UIAppFonts" Remember, if you have more than one plist file, set this for all.

    0 讨论(0)
  • 2021-01-13 09:32

    Matt did a great job of isolating the problem... I was not loading the font correctly.

    In my case the problem was in the Info.plist.

    I was listing the font in the Info.plist using its name. This is incorrect. You are supposed to list the font in the Info.plist using its file name.

    0 讨论(0)
  • 2021-01-13 09:33

    Issue is You are not giving ProperName Of Fonts in Code, for Getting Name of Fonts please run this code.

     for family: String in UIFont.familyNames
        {
            print("\(family)")
            for names: String in UIFont.fontNames(forFamilyName: family)
            {
                print("== \(names)")
            }
        }
    

    it will prints all fonts, search your font and give proper name.

     let attributedText = NSMutableAttributedString(string: "We 
     Apologize! \n This feature is only available for \n Go 4.0 
     Subscriptions", attributes: [NSAttributedStringKey.font : 
     UIFont(name: "SpecialElite-Regular", size: 22)!])
    
    0 讨论(0)
  • 2021-01-13 09:41

    When I try to use it programmatically it doesn't work, and it doesn't show up when I print out a list of available fonts

    This proves that you have not in fact included it properly in the target and the Info.plist.

    The reason it seems to work in IB is that this font is also present on your computer. But in fact if you were to run this app on your device, you would see that even setting the font in IB is not working.

    Your font is Special Elite. As you can see, I have it visible here in my running app:

    enter image description here

    Here's the code that I used:

        let lab = UILabel()
        lab.text = "This is a test"
        lab.font = UIFont(name:"SpecialElite-Regular", size:18)
        lab.sizeToFit()
        lab.frame.origin = CGPointMake(100,100)
        self.view.addSubview(lab)
    

    So you see, it is possible to refer to this font in code — if it is loaded properly. You are evidently not loading it properly. It's not in your app bundle, or it's not in the copy build phase, or it's not correctly listed in your Info.plist.

    (Of course there's always a possibility that you're calling [UIFont fontWithName:size:] with a bad value for the name or for the size.)

    0 讨论(0)
  • 2021-01-13 09:41

    As mentioned by others, following article is very useful to diagnose the problem. http://codewithchris.com/common-mistakes-with-adding-custom-fonts-to-your-ios-app/

    Also while adding custom fonts to Xcode project, add individual font and not the directory in which all fonts are stored. If you add that directory, fonts will be visible in Interface Builder but not in the running app.

    0 讨论(0)
提交回复
热议问题