2

I'm trying to add a new font to my iOS app. I've followed the following steps :

  • Add my font file to my "res" folder
  • Add my full font name to my plist file
  • Add a UILabel subclass with the following code

Code:

- (id)initWithCoder:(NSCoder*)aDecoder
{
    self = [super initWithCoder:aDecoder];
    if (self) {
        [self setFont:[UIFont fontWithName:@"Gotham-Book" size:self.font.pointSize]];
    }
    return self;
}

However the font is not displayed... I've try the following code to display all the fonts of my app and Gotham is not displayed...

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

I don't understand why it doesn't work. So if you can help me !

lootsch
  • 1,867
  • 13
  • 21
testoverblaireau
  • 179
  • 5
  • 19
  • Are you sure you are creating your object with your `initWithCoder:` method ? Maybe it's created with `init` or something else, that could be the reason why the font is not set. :) – Jissay Mar 17 '14 at 10:55
  • 1
    @Jissay if you just set the class of your label to be your custom label class name in IB, initWithCoder is automatically being called. – Anindya Sengupta Mar 17 '14 at 11:02
  • Oh, okay i thought it was something else, i've learned something, thanks ! – Jissay Mar 17 '14 at 11:09

4 Answers4

3

I hope you have completed the following steps:

  1. Add the font in your Xcode project as a resource (you have mentioned this to be done)
  2. Add the name of the file (including extension) in the plist file key "Fonts provided by application" enter image description here
  3. Check if the file is included in "Copy Bundle Resources" in Xcode project properties under Build Phases

If all of the above is done, then the mistake you made is the name of the font will be "Gotham Book" and not "Gotham-Book". Code below:

-(id)initWithCoder:(NSCoder *)aDecoder
{
    self = [super initWithCoder:aDecoder];
    if (self) {
        [self setFont:[UIFont fontWithName:@"Gotham Book" size:self.font.pointSize]];

    }
    return self;
}
Anindya Sengupta
  • 2,539
  • 2
  • 21
  • 27
  • The file was not in "Copy Bundle Ressources". Now it works, Thx a lot! – testoverblaireau Mar 17 '14 at 11:13
  • Glad to be of help. An interesting input: to find out the correct name of the font, open the installed font in Font Book, select it and press cmd+I. The font inspector will reveal the full name of the font along with many other useful information. – Anindya Sengupta Mar 17 '14 at 11:28
0

You're saying you have added the font name to the plist. However you need to add the filename to the plist, not the font name. Hope this helps.

Lennart
  • 61
  • 7
0

Possible duplicate of:

Can I embed a custom font in an iPhone application?

Check samvermettes answers.

Ensure you are using the correct family name, this isn't the name of the font but rather the name that appears when you install the font, this can be different. If you install the font on your machine you can check the name there.

Community
  • 1
  • 1
CW0007007
  • 5,681
  • 4
  • 26
  • 31
-2

you should add font name in plist file under "Fonts provided by application" And then (if not exist please add)

#define Font_Gotham_Book(s) [UIFont fontWithName:@"Gotham-Book" size:s];

And use,

lbltest.font = Font_Gotham_Book(15);
Khalil
  • 238
  • 2
  • 15