UIFont with custom font fails with Nil

后端 未结 7 1887
孤街浪徒
孤街浪徒 2020-12-31 10:59

I\'m trying to add a custom font to my project in Xcode 4.2, but whenever I try to use it, I get a error that the object is nil.

I have done the following:
1) Ad

相关标签:
7条回答
  • 2020-12-31 11:14

    I had this exact same problem for a few hours and tried all of the above and none worked, the font was being added everywhere it should have been and I even tried fonts which worked in other apps, but never appeared to be added to this app.

    It appears something has changed in Xcode 5 the font had to be added in the target properties under the info tab for this to work whereas previously they had to be added in the "appName-info.plist' see image below:

    Hope this helps someone else.

    enter image description here

    0 讨论(0)
  • 2020-12-31 11:16

    In my case, I needed to remove all the extra hyphens from the font name, aside from the one right before the font weight:

        UIFont(name: "SF-Pro-Display-Semibold", size: 15) // fails
        UIFont(name: "SFProDisplay-Semibold", size: 15) // succeeds
    
    0 讨论(0)
  • 2020-12-31 11:17

    The space in the Full Name of the font was removed automatically after adding it to the project. I've checked the original font file, and the space is there, so Apple must not want to deal with spaces. I changed the reference to "LCDMono2Ultra" and it works.

    0 讨论(0)
  • 2020-12-31 11:21

    My font file is "Futura Heavy.ttf", then I use - replaced the space like below then it works!

        class func futuraHeavy() -> UIFont {
            return UIFont(name: "Futura-Heavy", size: 12)!
        }
    
    0 讨论(0)
  • 2020-12-31 11:27

    Be sure to include the font's file extension as part of the name in plist "Fonts provided by application".

    0 讨论(0)
  • 2020-12-31 11:28

    Have you put a breakpoint in and checked that you are getting a valid UIFont object for myFont. This is worth checking as most likely the issue is that "LCDMono2 Ultra" is actually the font family name and what you want to be using is something like "LCDMono2 Ultra-normal" or "LCDMono2 Ultra-bold". Open your .ttf with Font Book and see what info it gives you.

    Alternatively if you can't find anything in Font Book you could try calling:

    NSArray *fonts = [UIFont fontNamesForFamilyName:"LCDMono2 Ultra"];
    

    And then just printing the array fonts in the debugger to check what they should be.

    Hope this help, let me know if this doesn't work and i'll try to think of something else :)

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