23

How would you add a custom font in Xcode 5 and how would you change every label in the project to that font? Because I've heard you can only do this programmatically?

Kara
  • 6,115
  • 16
  • 50
  • 57
Mr Big Problem
  • 269
  • 1
  • 3
  • 12

6 Answers6

70

You need to set every label programmatically with your custom Font.

To use custom font :

1/ add your custom font in your project like resources (font .ttf or .otf)

2/ in your info.plist add key UIAppFonts (Fonts provided by application) and and the name of each custom font (for example : SohoGothicStd.ttf)

3/ you can create macro for use your font

#define FONT_SOHO_STD(s) [UIFont fontWithName:@"SohoGothicStd" size:s]

4/ use this macro for a label par exemple :

_myLabel.font = FONT_SOHO_STD(15.0f);
Jordan Montel
  • 8,227
  • 2
  • 35
  • 40
  • Nice - for simple case this woks and it is easy to use. For complex part you should follow subclass methods. – Bishal Ghimire Sep 22 '13 at 08:49
  • Do not use define for string values! It will copy it for each place where macro using. You need to use `extern NSString* const myFontName` – Ilya Ilin Feb 21 '14 at 01:19
  • Using `const` you have a reference to the memory. With `#define` you replace your macro before the compilation start. So I prefer use `#define` and for me it's more readable in your code. – Jordan Montel Feb 21 '14 at 07:27
  • 1
    You don't need to set each UILabel individually. Use appearance to set the default like so: [[UILabel appearance] setFont:[UIFont fontWithName:@"SohoGothicStd" size:15]]; I still like your macro as it allows for easy setting of different sizes. – n13 Apr 01 '14 at 03:58
  • Also do remember that font file name != font name. – AndrewShmig May 03 '14 at 19:40
  • @AndrewShmig I ran into the problem of not specifying the correct font name, because it didn't match the file name. To clarify the font name after you've completed steps 1 & 2, you can simply print a list of the font names to the console using the loop found in [this tutorial](http://bit.ly/1knlZYt). From there, you can insert that name in step 3. – meghaphone Jun 11 '14 at 18:41
  • 1
    @meghaphone, just Cmd+I on font file :) No need to iterate over all fonts and find needed one. – AndrewShmig Jun 11 '14 at 19:20
15

I believe you need to use [UILabel appearance] proxy to set custom font for all labels across your application. Add following lines to your AppDelegate didFinishLaunchingWithOptions function to set custom font for all UILabels in your project.

UIFont *newFont = [UIFont fontWithName:@"My-Custom-Font-Name" size:14];
[[UILabel appearance] setFont:newFont];

NOTE: You need to make sure your fonts are in your Xcode project.

Here are the steps you can follow to add custom font to the project.

  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

Steps taken from: Can I embed a custom font in an iPhone application?

Community
  • 1
  • 1
Yas Tabasam
  • 10,517
  • 9
  • 48
  • 53
5

Yes you need to do it through code. I dont think XCode5 has added any new feature like such. To do it programmatically do the following :

Make a class which is subclass of UILabel then say you have something like Title which has font size 14 with Helverica-Bold

IN .h file

#import <UIKit/UIKit.h>

@interface MyLabel : UILabel {

}

@end

In .m file

    #import "MyLabel.h"

    @implementation MyLabel

    - (id)initWithCoder:(NSCoder *)coder {
    self = [super initWithCoder:coder];
    if (self) {
        self.font = [UIFont fontWithName:@"Helvetica-Bold" size:14];
    }
    return self;
}

    @end

Another example without using separate sub class

-(UILabel *) myTitleLabel {
    UILabel *label = [[UILabel alloc] init];
    label.backgroundColor=[UIColor clearColor];
    [label setFont:[UIFont fontWithName:@"Helvetica-Bold" size:14]];
    label.textColor=[UIColor colorWithRed:(79.0 / 255.0) green:(79.0 / 255.0) blue:(79.0 / 255.0) alpha: 1];
    [label setTextAlignment:NSTextAlignmentLeft];
    [label sizeToFit];
    return label;
}

Then you can use myTitleLabel class to create a object so that all the titleLabel Object will have same color and font size.

Bishal Ghimire
  • 2,580
  • 22
  • 37
2

Much better option is to add the new font to your system/OS fonts directory and then from there it can be added in the storyboard or used with NSAttributed strings.

Steps:

  1. Download your font .ttf files
  2. Go to any label/textview in your storyboard and select in top right attributes inspector: "Text" Then select > "Attributed"
  3. Click the little T icon next to the font name to open up the fonts editor
  4. Go to the cog/wheel/setting icon and select "Manage Fonts"
  5. Click the second from right "+" button and navigate to your fonts .ttf file to add to system fonts collection
  6. Now use your font by changing the font name in the storyboard or code :)

Hope that helps!

1

The answers here are fine, but I'd recommend setting as little as possible in your custom label and definitely not hardcoding the size. You can easily do it like this:

#import "CustomFontLabel.h"

@implementation CustomFontLabel

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setFontStyle];
    }
    return self;
}

- (id)initWithCoder:(NSCoder *)aDecoder {
    self = [super initWithCoder:aDecoder];

    if (self) {
        [self setFontStyle];
    }

    return self;
}

- (void)setFontStyle {
    CGFloat fontSize = self.font.pointSize;

    [self setFont:[UIFont fontWithName:@"LindenHill" size:fontSize]];
}

@end

This will respect all of the other settings you set in your Storyboard making it way easier to manage everything.

Alper
  • 3,424
  • 4
  • 39
  • 45
  • The above solution worked great. But the fontsize is set to 17 even though i have set the font size to 22 for UILabel in the xib. Can you tell me if i am missing something? cuz i need varying font sizes . – Nitesh Nov 16 '13 at 07:41
  • That's super odd, it should respect the font size you set and only override the font… – Alper Nov 22 '13 at 16:13
  • i could resolve the issues from changing the Font from System to Custom. Thanks for your help – Nitesh Nov 24 '13 at 13:07
1

You can create custom font in Xcode using following steps :

  1. Import font type into your project (*.ttf file)
  2. Add “Fonts provided by application” key into plist of your project.
  3. Add custom font as an item array in “Fonts provided by application” key.
  4. Now, you can use font in label as :

    [lblName setFont:[UIFont fontWithName:@“CustomFontName” size:14.0]];

Jayprakash Dubey
  • 35,723
  • 18
  • 170
  • 177