Custom Font in ios not working

前端 未结 17 1848
北海茫月
北海茫月 2020-12-07 22:53

I want to use HelveticaNeue-UltraLight in my ios application. I\'m adding the font file as a resource to my project and adding the \"Fonts provided by application\" key in t

相关标签:
17条回答
  • 2020-12-07 23:10

    Try with spaces:

    [UIFont fontWithName:@"Helvetica Neue UltraLight" size:12];
    

    See Certain fonts not showing up?

    0 讨论(0)
  • 2020-12-07 23:12

    I think you're having the same problem I had : there's a bug in iOS where it is not able to use more than 2 fonts from the same family.

    See blog post here for details and solution : http://www.pixeldock.com/blog/uifont-problem-when-using-more-than-2-custom-fonts-of-the-same-font-family/

    0 讨论(0)
  • 2020-12-07 23:12

    In addition to the well explained @Ben answer, if you're still not getting your fonts, Just use or assign your font to any Label in one of your Storyboard, You will start getting it using this code

    If you still unable to get font using [UIFont fontWithName:@"Font-Regular" size:11]; There must be problem in naming. Stop at any Breakpoint in debugger and Debug it using different names, and see which po prints object that is not nil

    po [UIFont fontWithName:@"FontRegular" size:11];

    or

    po [UIFont fontWithName:@"Font Regular" size:11];

    or

    po [UIFont fontWithName:@"Font-Regular" size:11];

    You will get <UICTFont: 0x7fe2044021d0> font-family: "Interstate-Regular"; font-weight: normal; font-style: normal; font-size: 11.00pt for actual font name otherwise you will get nil

    0 讨论(0)
  • 2020-12-07 23:12

    note that add .ttf end of each Fonts provided by application name

    0 讨论(0)
  • 2020-12-07 23:18

    It's an old question but I'd like to emphasize that we must write font file name WITH EXTENSION into info.plist. It was particularly my mistake: I missed extensions. But the very interesting thing that iOS successfully loaded three fonts even when the extensions were missed. Once I've added extensions all 12 fonts were loaded.

    Good article: https://codewithchris.com/common-mistakes-with-adding-custom-fonts-to-your-ios-app/

    0 讨论(0)
  • 2020-12-07 23:20

    It is working with different variations of the same font(even in IOS 8). I'll post my mistake just in case someone has the same problem... I was using font's filename instead of the font name. A useful solution is print all the fonts available and look for the one I was using. In Swift would be:

    for fontFamilyNames in UIFont.familyNames {
        for fontName in UIFont.fontNames(forFamilyName: fontFamilyNames) {
            print("FONTNAME:\(fontName)")
        }
    }
    

    I found this useful tutorial in: Code with Chris

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