Adding Custom Fonts to iOS App Finding Their Real Names

Adding custom fonts to iOS app finding their real names

Use +[UIFont familyNames] to list all of the font family names known to the system. For each family name, you can then use +[UIFont fontNamesForFamilyName:] to list all of the font names known to the system. Try printing those out to see what name the system expects. Example code:

static void dumpAllFonts() {
for (NSString *familyName in [UIFont familyNames]) {
for (NSString *fontName in [UIFont fontNamesForFamilyName:familyName]) {
NSLog(@"%@", fontName);
}
}
}

Put that in your app, call it, and see what you get. If you see a name in the output that looks appropriate for your font, use it. Otherwise, perhaps you haven't properly added the font to your app.

In Swift:

func dumpAllFonts() {
for familyName in UIFont.familyNames {
for fontName in UIFont.fontNames(forFamilyName: familyName) {
print(fontName)
}
}
}

How to use custom fonts in iPhone SDK?

  1. Add your custom font into your project , i.e. Dragged the font file(CALIBRIZ_0.TTF) into XCode project.

  2. Edit Info.plist: Add a new entry with the key "Fonts provided by
    application".

  3. For each of your files, add the file name to this array

    Sample Image

  4. Opened the font in font book(double click on your font in finder) to see what the real filename is and
    I see this:

Sample Image

Now set font to your label

yourLabel.font = [UIFont fontWithName:@"Calibri" size:15];

Follow this to get more

can't find the name of custom font added to xcode 7

Did you try something like:

[yourlabel setFont:[UIFont fontWithName:@"PT Sans" size:20]];

How to use Custom font in iPhone

You have to add custom fonts if its not available in the list that Apple provides. A very good tutorial to learn how to use custom fonts in iOS.

How can I get the font names bundled in my iOS app?

This is blatently ripped off from another answer
It should provide you with all the font names from your bundled fonts.

NSDictionary* infoDict = [[NSBundle mainBundle] infoDictionary];
NSArray* fontFiles = [infoDict objectForKey:@"UIAppFonts"];

for (NSString *fontFile in fontFiles) {
NSLog(@"file name: %@", fontFile);
NSURL *url = [[NSBundle mainBundle] URLForResource:fontFile withExtension:NULL];
NSData *fontData = [NSData dataWithContentsOfURL:url];
CGDataProviderRef fontDataProvider = CGDataProviderCreateWithCFData((__bridge CFDataRef)fontData);
CGFontRef loadedFont = CGFontCreateWithDataProvider(fontDataProvider);
NSString *fullName = CFBridgingRelease(CGFontCopyFullName(loadedFont));
CGFontRelease(loadedFont);
CGDataProviderRelease(fontDataProvider);
NSLog(@"font name: %@", fullName);
}

This answer would get you the proper font name, but if you're just wanting the font name from the info plist:

NSDictionary* infoDict = [[NSBundle mainBundle] infoDictionary];
NSArray *arrayOfFontStringNames = infoDict[@"UIAppFonts"];

How do I get the font name from an otf or ttf file?

Follow these four easy steps to add and use a new font in your iOS app:

  • Add your_new_font.ttf or your_new_font.otf to your Xcode project
  • In your project's info.plist, add a new entry for your_new_font.ttf or your_new_font.otf to the UIAppFonts array (plain text for this one is 'Fonts provided by application')
  • At this point, I recommend adding this temporary chunk of debug code to dump all fonts that are accessible by your app, including your newly added your_new_font:

//Swift

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

//Objective-c

for(NSString *fontfamilyname in [UIFont familyNames]) {
NSLog(@"family:'%@'",fontfamilyname);
for(NSString *fontName in [UIFont fontNamesForFamilyName:fontfamilyname]) {
NSLog(@"\tfont:'%@'",fontName);
}
NSLog(@"-------------");
}
  • In the debug output, look for your new font's 'family' and 'font' name. Pass whatever is displayed as the 'font' name corresponding to your new font family (there could be more than one 'font' associated with your new font 'family') to UIFont *myNewFont = [UIFont fontWithName:@"font_name_from_debug_output" size:20] and you should be in business!

Can I embed a custom font in an iPhone application?

iOS 3.2 and later support this. Straight from the What's New in iPhone OS 3.2 doc:

Custom Font Support

Applications that want to use custom fonts can now include those fonts in their application bundle and register those fonts with the system by including the UIAppFonts key in their Info.plist file. The value of this key is an array of strings identifying the font files in the application’s bundle. When the system sees the key, it loads the specified fonts and makes them available to the application.

Once the fonts have been set in the Info.plist, you can use your custom fonts as any other font in IB or programatically.

There is an ongoing thread on Apple Developer Forums:

https://devforums.apple.com/thread/37824 (login required)

And here's an excellent and simple 3 steps tutorial on how to achieve this (broken link removed)

  1. Add your custom font files into your project using Xcode as a resource
  2. Add a key to your Info.plist file called UIAppFonts.
  3. Make this key an array
  4. For each font you have, enter the full name of your font file (including the extension) as items to the UIAppFonts array
  5. Save Info.plist
  6. Now in your application you can simply call [UIFont fontWithName:@"CustomFontName" size:12] to get the custom font to use with your UILabels and UITextViews, etc…

Also: Make sure the fonts are in your Copy Bundle Resources.

Custom fonts on iOS app - working in Simulator but not iPad

I can only guess as you haven't posted the contents of your plist or a directory listing of the bundle, but many cases of "resource works on the simulator but not on the device" are caused by the fact that the OS X filesystem is normally configured to be case-insensitive while the filesystem on the device is case sensitive. For example, if your file is named "BlackChancery.TTF" and your plist refers to it as "BlackChancery.ttf", it will be found on the simulator but not on the device.

SwiftUI won't display custom font

If you have made sure that the Info.plist is using the correct filename:

Sample Image

Note if you are using Xcode 13 you may not have an Info.plist where you expect. This SO answer explains where you can find it.

That the font is available in the app's target.

Sample Image

You also need to make sure that you are accessing the font by the correct name.

An easy way to check the font's name is to add the following to your AppDelegate in the didFinishLaunchingWithOptions before the return true. Or if you are using the new SwiftUI lifecycle you can add it to an .onAppear.

for family in UIFont.familyNames.sorted() {
let names = UIFont.fontNames(forFamilyName: family)
print("Family: \(family) Font names: \(names)")
}

This will list all the fonts by family and name.

Just remember to remove it once you have finished using it as you don't need to unnecessarily print to the console.

When I do it for my fonts (I have added the same font as you) I find the following in the console in the list of available fonts (see the above screenshot) :

Family: Helvetica Now Display Font names: ["HelveticaNowDisplay-Bold"]

Your font may have a different name to mine, and it is important to note that the font name may not be the same as the filename. This is what trips up a lot of people, as they try using the filename when they need to use the font name.

The following test code produces:

struct ContentView: View {
var body: some View {
Text("Hello")
.foregroundColor(.blue)
.font(Font.custom("HelveticaNowDisplay-Bold", size: 60))
}
}

Sample Image

For more information about adding custom fonts see Apple's documentation.



Dynamic Type in SwiftUI

If you are using a custom font then you should consider setting it up so that it will scale with dynamic type.

iOS 14

iOS 14 introduces a new modifier that allows you to scale a font relative to a Dynamic Font Type.

Text("Hello")
.font(.custom("HelveticaNowDisplay-Bold", size: 60, relativeTo: .body))

iOS 13

If you are using iOS 13 that requires a bit more effort to get the same effect.

You first need to create a ViewModifier. This view modifier listens to the size category from the environment (it doesn't actually use it but having it here makes sure the view modifier is updated whenever the size category is updated).

struct ScaledFont: ViewModifier {
@Environment(\.sizeCategory) var sizeCategory
var name: String
var size: CGFloat

func body(content: Content) -> some View {
let scaledSize = UIFontMetrics.default.scaledValue(for: size)
return content.font(.custom(name, size: scaledSize))
}
}

extension View {
func scaledFont(name: String, size: CGFloat) -> some View {
return self.modifier(ScaledFont(name: name, size: size))
}
}

It is then used in the following way:

Text("Hello")
.scaledFont(name: "HelveticaNowDisplay-Bold", size: 60)

For a really good write up check out this post on Hacking With Swift.

custom font becomes a nil value in swift 3

Try this

let attribute1:[String:Any] = [NSFontAttributeName: UIFont(name: "Bariol-Bold", size: 18)! ,
NSForegroundColorAttributeName : UIColor.init(colorLiteralRed: 120.0/255, green: 173.0/255, blue: 194.0/255, alpha: 1.0)]

let attributedStringGreeting=NSAttributedString.init(string: welcomeMessage, attributes: attribute1)


Related Topics



Leave a reply



Submit