Change the default systemFont used by controls

浪子不回头ぞ 提交于 2019-12-21 23:29:16

问题


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?


回答1:


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



回答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



来源:https://stackoverflow.com/questions/13798450/change-the-default-systemfont-used-by-controls

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!