8

I've added my custom font to UIAppFonts and it's loaded just fine: (shows up in [UIFont familyNames] ). When I manually set the font in viewDidLoad { [myLabel setFont: [UIFont fontWithName:@"CustomFont" size: 65.0]]; } everything works and the font is rendered.

However doing the same thing in IB doesn't (some other default font is used instead). Having to create IBOutlets for each label and fixing up the fonts manually in viewDidLoad is pretty painful.

Anyone else had problems getting the custom font support to work with 3.2 SDK and IB?

Douwe Maan
  • 6,770
  • 2
  • 32
  • 34
Tarmo
  • 5,864
  • 2
  • 17
  • 12

3 Answers3

2

Opened a bug report with Apple and turns out it really is a bug. The workaround I ended up using is this:

// LabelQuake.h
@interface LabelQuake : UILabel
@end

// LabelQuake.m
@implementation LabelQuake

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

        return self;
    }
@end

Wrote a bit longer post at our blog.

Tarmo
  • 5,864
  • 2
  • 17
  • 12
  • When using your solution above, it works but for some reason, all the labels that are custom do not center the text vertically in the label -- instead they all snap the text to the top. Did you notice this? It has the effect of offsetting all my labels and making it all look out of line. – Andrew Jan 05 '11 at 21:57
  • 1
    In response to my own comment above, I "fixed" the issue by checking self.font.ascender before setting the new font, and then offsetting the whole label by the difference _after_ you set the font. – Andrew Jan 05 '11 at 22:13
2

had simillar kind of problem.and fixed it in this way...

add my custom font to my Resource group. then load all the fonts by the code given bellow:

- (NSUInteger) loadFonts{
NSUInteger newFontCount = 0;
NSBundle *frameworkBundle = [NSBundle bundleWithIdentifier:@"com.apple.GraphicsServices"];
const char *frameworkPath = [[frameworkBundle executablePath] UTF8String];
if (frameworkPath) {
    void *graphicsServices = dlopen(frameworkPath, RTLD_NOLOAD | RTLD_LAZY);
    if (graphicsServices) {
        BOOL (*GSFontAddFromFile)(const char *) = dlsym(graphicsServices, "GSFontAddFromFile");
        if (GSFontAddFromFile)
            for (NSString *fontFile in [[NSBundle mainBundle] pathsForResourcesOfType:@"ttf" inDirectory:nil])
                newFontCount += GSFontAddFromFile([fontFile UTF8String]);
    }
}

return newFontCount;}


 - (id)initWithCoder:(NSCoder *)decoder {
    //load the fonts
    [self loadFonts];

    if (self = [super initWithCoder: decoder]) {
        [self setFont: [UIFont fontWithName: @"Quake" size: self.font.pointSize]];
    }

    return self;
}

Hope it will work.

makboney
  • 1,814
  • 1
  • 14
  • 33
  • 1
    didn't adding your fonts to UIAppFonts property in Info.plist cause them to be loaded? – Tarmo Jun 08 '10 at 10:38
  • this little hack you have here makes me very happy :) With this I can use custom fonts with ease when targeting iOS < 3.2. Is it using a private framework? – Aran Mulholland Sep 13 '10 at 12:47
  • what are these values RTLD_NOLOAD | RTLD_LAZY?? – prajakta Jan 28 '11 at 06:52
  • @prajakta...sorry for late reply...better have a look on [apple reference](http://developer.apple.com/library/mac/#documentation/Darwin/Reference/ManPages/man3/dlopen.3.html) – makboney Feb 22 '11 at 06:49
1

If you don't want to have to subclass, this solution worked quick and dirty for me. Of course it assumes that all labels have the same font, and in my case that was the case.

for (UIView *v in view.subviews) {

    if ([v isKindOfClass:[UILabel class]]) {
      UILabel *label = (UILabel*)v;

      [label setFont:[UIFont fontWithName:@"Quake" size:label.font.pointSize]];
    }
}

I put this in a helper class and just called it, passing in my current view.

Andrew
  • 1,447
  • 1
  • 14
  • 22
  • rather than changing EVERY font. would setting the UIView.tag for just the labels you want to change and then checking it in this loop also work – brian.clear Feb 21 '12 at 13:18