Change the default systemFont used by controls

我是研究僧i 提交于 2019-12-04 16:07:44
Paramasivan Samuttiram

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

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

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