2

I have a big App and need to change the Font but I don't want to touch every Label, Textfield and so on. How can I access the "systemFont" used in IB and in [UIFont systemFontOfSize:x]?

I already tryed this one: iOS 5: Curious about UIAppearance but that's not the solution, cause I have different FontSizes and Bold/Regular all over my App and I can't set it overall.

Can I set it via UIApplication or somewhere in InfoPlist?

Community
  • 1
  • 1
xapslock
  • 1,119
  • 8
  • 21
  • it would be nice if there was an easy way to do this.. – Ben Clayton Dec 10 '12 at 09:50
  • If this is a one time thing, and you just want to use a different font than the one that you used in your application's interface using IB, you can try to edit the XIBs by hand since it's XML... If your interface is in code and you want to change the behavior of `systemFontOfSize:` *everywhere*, then maybe you can look at method swizzling... I have never tried that though, there might be side effects... – Guillaume Dec 10 '12 at 10:07
  • Unfortunately its not a one time thing. – xapslock Dec 10 '12 at 10:20

2 Answers2

5

You can create Category for UILabel, UITextField and UIButton and can in the "awakeFromNib" method, you can just change the font name to new font and can just keep the same size. I added code for UILabel category below. You can do the same for UITextField and UIButton.

@implementation UILabel (CustomFontLabel)

-(void)awakeFromNib{
    [super awakeFromNib];
    float size = [self.font pointSize];
    NSString *stringfontstyle=self.font.fontName;
    if([stringfontstyle rangeOfString:@"Bold"].location != NSNotFound) {
        self.font = [UIFont fontWithName:@"MyCustomFont-Bold" size:size];
    }
    else if ([stringfontstyle rangeOfString:@"Italic"].location != NSNotFound) {
        self.font = [UIFont fontWithName:@"MyCustomFont-Italic" size:size];
    }
    else if ([stringfontstyle rangeOfString:@"Medium"].location != NSNotFound) {
        self.font = [UIFont fontWithName:@"MyCustomFont-Medium" size:size];
    }
    else {
        self.font = [UIFont fontWithName:@"MyCustomFont" size:size];
    }
}

@end
xapslock
  • 1,119
  • 8
  • 21
Paramasivan Samuttiram
  • 3,728
  • 1
  • 24
  • 28
  • That's a good one. Did you know an easy way to check if its bold or not? Or is the only way to check the fontname for "-bold"? – xapslock Dec 10 '12 at 10:21
  • This solution would be better as a subclass than a category. A category does not override methods of a class, it replaces them. The call to ```[super awakeFromNib]``` won't call UILabel's original awakeFromNib implementation, but it will call its superclass's ```UIView``` implementation. This will cause problems if Apple adds any code to UILabel's default awakeFromNib implementation in the future. – PPierson Sep 22 '14 at 16:56
2

Actually the answer from Paramasivan is only partially right. The problem is that if for example UILabel were to implement awakeFromNib it would never get called. Your category would override this method. You can either swizzle or override it like shown here:

http://www.mikeash.com/pyblog/friday-qa-2010-01-29-method-replacement-for-fun-and-profit.html

Danilo
  • 3,257
  • 2
  • 19
  • 24