Custom Font in ios not working

浪子不回头ぞ 提交于 2019-11-28 04:19:49
Ben G

Not only does the file needs to be added to the project, but it needs to be added to the target as well.

Sometimes (as in my case), when drag and dropping the ".ttf" file in Xcode, the "add to target" is not checked. This means the file is actually visible in your project, but it is not embedded in the app.

To make sure it is:

  • Click on the project's name (left pane)
  • Then on the target (middle pane)
  • Then "Build Phases" (third tab on the right pane)
  • Then "Copy Bundle Resources"

You should see the font file in that list.

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/

after adding Font file in Bundle "Font Provided by Application" property click on drop down arrow and enter your font name with extension

Open Font Book application. If you installed the fonts yourself, go to user, look for the fonts you want and use the PostScript name of the font in your xcode project.

It should work even for different font variations of the same family.

TomCobo

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

While this probably won't help the OP, it might be useful to others.

With iOS 5, Helvetica Neue is automatically included; you don't have to add this custom font to the bundle. It's even possible to use that font family directly from Interface Builder, which is extremely handy.

Try this this will be also heleful for you.

    [compete_Label setFont:[UIFont fontWithName:@"Helvetica-Bold" size:12]];
Jano

Try with spaces:

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

See Certain fonts not showing up?

I ended up buying the font Helvetica Neue UltraLight from a third party, added it with no problems.

After spending hours, I came to know that the Name for the font we need to add is not the file name of the font but the actual Font name. I was trying to include font file bonveno.ttf. I have included the filename along with extension in info.plist and was able to see the font in the Copy Bundle Resources list. My code was like

self.label.font = [UIFont fontWithName:@"bonveno" size:30];

that was causing the problem. Then I double clicked the font file from Finder to see the preview of the font. That time I noticed the name of the font in the preview window as BonvenoCF. So I added that name in the code like

self.label.font = [UIFont fontWithName:@"BonvenoCF" size:30];

Worked like charm!! Can't live without this cute font in my apps.

Mahesh

In order to use custom fonts in iOS, just add your fonts by drag & drop to your project. Mention the array of font names with extension in Info.plist with key "Fonts provided by application". Then print all font family names using below code :

for (NSString* family in [UIFont familyNames])

{

    DebugLog(@"FONT FAMILY: %@", family);

    for (NSString *name in [UIFont fontNamesForFamilyName: family])
    {
        DebugLog(@"  %@", name);
    }
}

Console print :

Arial Hebrew ArialHebrew-Bold ArialHebrew-Light ArialHebrew

Calibri Calibri-Bold Calibri

Georgia Georgia-BoldItalic Georgia-Bold Georgia-Italic Georgia

Then from console, you will get the actual font-names (As above). Use below code to create font -

In core text :

CTFontRef fontRef = CTFontCreateWithName((CFStringRef)fontName, fontSize, NULL);

In UIKit :

UIFont *font = [UIFont fontWithName:fontName size:10];

Note : here fontName = @"Calibri-Bold" is without extension.

In addition to:

  • adding the font file names to the info.plist under "Fonts provided by application"
  • adding the actual files to the project.

You also have to make sure that under Targets -> Build Phases -> Copy Bundle Resources has the actual filenames of the custom fonts.

To check if they have appeared, you can do something like:

        UIFont.familyNames.forEach { (name) in
        print(name)
    }

Another thing to check is on a Mac: Open the app Font Book. Then go file -> file validation and open the font. If something is failing (even with a warning) in there iOS generally rejects loading the font in the app with no errors.

If this is the case then you can fix the errors by opening the font in a free app called "Typelight" And resaving the font "Save as".

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!