15

I am currently working on an iPhone project in which all labels/text views etc. should appear with a custom font (I'm using Xcode 4.2.1). I have done some research on this, and the only solution seems to be adding the font files to the project, editing the info.plist file appropriately and setting the font programmatically (i.e. by outlets or by subclassing the relevant views). In any case, these approaches won't lead to the interface builder displaying the text with the custom font (it will still show Helvetica). It is not possible to select the custom font using the attributes inspector.

Although I don't think I missed something, I just want to make sure there is no other way than programmatically setting the fonts, which would be a rather painful for the task I have to work on (localization issues, unique app design etc.). It would be nice to get the interface builder to display custom fonts..

8 Answers 8

43

Custom fonts can only be set programmatically..Unfortunately..this is termed as a bug in interface builder and hasn't been fixed yet.

4
  • more than a year passed and its still not fixed. I can see attributed text with new font in Attributes Inspector but the label doesn't update. changing more than 100 labels programmatically will be a pain. :(
    – Sam
    Sep 13, 2013 at 7:45
  • 3
    Fixed with xcode 6 added support for custom apps in IB
    – kush
    Nov 25, 2014 at 8:13
  • 1
    The fact that the attributed text doesn't work from IB is completely ridiculous ... it should be omitted from IB. What a blunder :/ Jun 4, 2015 at 16:51
  • A shame in fact!!
    – Falcoa
    Nov 7, 2016 at 17:43
6

using custom fonts in Interface Builder a reusable library is available.

It uses a simple trick for doing this. Set the font(say calibri) to all of your UI Elements that you will never use in your project and FontReplacer will do mapping between custom font and calibri. So there is no need to make IBOutlets or any other graphics.

Here is link to github from where you can download FontReplacer to use in your project. https://github.com/0xced/FontReplacer

another question is addressing same Fonts not displaying in Interface Builder i have also posted possible solution there

3
  • Warning: I can't find a license for FontReplacer, which means it might not be usable in your project at this point without contacting the code's author. The technique looks simple enough, though.
    – mjh
    Mar 5, 2013 at 0:27
  • 1
    License aside...has anyone been approved by Apple and shipped a product using this FontReplacer?
    – Tony
    May 29, 2013 at 22:31
  • 2
    I know at least one person who got app rejected by Apple because of FontReplacer. Instead use this category which was approved recently by Apple: github.com/deni2s/IBCustomFonts Oct 4, 2013 at 23:53
2

As of XCode 6 it now sees the font that I needed to import (Lato).

Haven't tested it much, but works for plain text labels and doesn't for attributed ones. Atrributed shows fine in Interface Builder but defaults to system font at runtime.

Still much better to have it there visually or combine text labels to achieve "attributed" text though!

1

It's really inconvenient. While waiting for this feature, I think this will help you organize your code regarding the font in iOS project.

In the shared class you use to define some global variables like Common.h, you define the fonts you may use

#define FONT_LATO_REGULAR(s) [UIFont fontWithName:@"Lato-Regular" size:s]
#define FONT_LATO_LIGHT(s) [UIFont fontWithName:@"Lato-Light" size:s]
#define FONT_LATO_BOLD(s) [UIFont fontWithName:@"Lato-Bold" size:s]

Then you import the Common.h (your shared class) into class you are implementing, set font for label by:

_lblTitle.font = FONT_LATO_BOLD(14.0);

Finally, you can put all font settings into a method for further modification.

- (void) setFontForLabels {
    _lblTitle.font = FONT_LATO_BOLD(14.0);
    _lblTime.font = FONT_LATO_REGULAR(13.0);
    _lblLocation.font = FONT_LATO_REGULAR(13.0);
}

It may help you a little. Please make sure you imported your fonts into splist file already.

1

Applicable for iOS 5.0 & Above.

Create a custom UI-Element(UIButton, UILabel, UITextField etc) class & simply change the class name for the particular element in storyboard for which you want to reflect the change.

Below is the code for custom button. You can do the same for any other element you want.

CustomButtton.h

@interface CustomButtton : UIButton

@end

CustomButtton.m

@implementation CustomButtton

    - (void)awakeFromNib {
        // Initialization code

        if ([self isFontBold:self]) {

            self.titleLabel.font = [UIFont fontWithName:@"LaoUI-Bold" size:self.titleLabel.font.pointSize];

        } else {

            self.titleLabel.font = [UIFont fontWithName:@"LaoUI" size:self.titleLabel.font.pointSize];
        }

        NSLog(@"Fonts:%@",[UIFont familyNames]);
    }

    -(BOOL)isFontBold:(UIButton *)sender {

        UIFont *font = self.titleLabel.font;
        UIFontDescriptor *fontDescriptor = font.fontDescriptor;
        UIFontDescriptorSymbolicTraits fontDescriptorSymbolicTraits = fontDescriptor.symbolicTraits;
        return (fontDescriptorSymbolicTraits & UIFontDescriptorTraitBold);
    }

@end

Now simply set the font size & type (Bold or Normal). It will automatically update the element with custom font.

By : [UIFont familyNames] - You can check whether your custom font family is included by the application or not which you have defined in .plist file.

Secondly: You have to add a key in .plist file of your project as shown in below screenshot:

.plist change for custom font support in application

0
0

I haven't tried this with earlier versions, but with XCode 6, If you have the font installed correctly in your project AND you install the fonts on your Mac, by using Font Book for instance, XCode shows the fonts in the font picker, allows you to set multiple custom fonts and sizes in a single label, and seems to actually use the correct fonts at runtime.

It's not all roses. Interface Builder's attribute controls are a still a mess. It frequently loses track of the assigned properties, particularly if you try to attribute more than a single paragraph at once. Font colors don't stick very well. Overlapping selections just confuse it incredibly, for instance if your label has two paragraphs, with two different fonts and you select both and change the font size, it may set the attributes of one or both paragraphs to a system default.

0

You can set custom font in the storyboard or xib with attibutes inspector, please ref to Custom font in a storyboard?

-4

You can use a simple trick to use custom fonts in interface builder. Here is descriptive tutorial that might help you in this regard Click Here for tutorial

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.