问题
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 the plist file. The file name is HelveticaNeue.dfont and I've added it to the key array.
When I check for the available fonts I can see it now...
NSArray *fonts = [UIFont fontNamesForFamilyName:@"Helvetica Neue"];
for(NSString *string in fonts){
NSLog(@"%@", string);
}
1-07-08 17:32:57.866 myApp[5159:207] HelveticaNeue-Bold
2011-07-08 17:32:57.866 myApp[5159:207] HelveticaNeue-CondensedBlack
2011-07-08 17:32:57.867 myApp[5159:207] HelveticaNeue-Medium
2011-07-08 17:32:57.867 myApp[5159:207] HelveticaNeue
2011-07-08 17:32:57.868 myApp[5159:207] HelveticaNeue-Light
2011-07-08 17:32:57.868 myApp[5159:207] HelveticaNeue-CondensedBold
2011-07-08 17:32:57.868 myApp[5159:207] HelveticaNeue-LightItalic
2011-07-08 17:32:57.869 myApp[5159:207] HelveticaNeue-UltraLightItalic
2011-07-08 17:32:57.869 myApp[5159:207] HelveticaNeue-UltraLight // HERE IT IS!
2011-07-08 17:32:57.869 myApp[5159:207] HelveticaNeue-BoldItalic
2011-07-08 17:32:57.870 myApp[5159:207] HelveticaNeue-Italic
But when I try to get it with [UIFont fontWithName:@"HelveticaNeue-UltraLight" size:12] I just get HelveticaNeue-Light..
I'm getting no errors or warnings. Help please!
回答1:
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.
回答2:
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/
回答3:
after adding Font file in Bundle "Font Provided by Application" property click on drop down arrow and enter your font name with extension
回答4:
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.
回答5:
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
回答6:
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.
回答7:
Try this this will be also heleful for you.
[compete_Label setFont:[UIFont fontWithName:@"Helvetica-Bold" size:12]];
回答8:
Try with spaces:
[UIFont fontWithName:@"Helvetica Neue UltraLight" size:12];
See Certain fonts not showing up?
回答9:
I ended up buying the font Helvetica Neue UltraLight from a third party, added it with no problems.
回答10:
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.
回答11:
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.
回答12:
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)
}
回答13:
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".
回答14:
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
来源:https://stackoverflow.com/questions/6631292/custom-font-in-ios-not-working