更改控件使用的默认系统字体

更改控件使用的默认系统字体

本文介绍了更改控件使用的默认系统字体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个很大的应用程序,需要更改字体,但我不想触摸每个标签、文本字段等.如何访问 IB 和 [UIFont systemFontOfSize:x] 中使用的systemFont"?

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]?

我已经尝试过这个:iOS 5: Curious about UIAppearance 但事实并非如此解决方案,因为我的应用程序中有不同的 FontSizes 和 Bold/Regular,我无法整体设置.

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.

我可以通过 UIApplication 或 InfoPlist 中的某处设置它吗?

Can I set it via UIApplication or somewhere in InfoPlist?

推荐答案

您可以为 UILabel、UITextField 和 UIButton 创建 Category 并且可以在awakeFromNib"方法中,您只需将字体名称更改为新字体即可相同的大小.我在下面为 UILabel 类别添加了代码.您可以对 UITextField 和 UIButton 执行相同的操作.

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

这篇关于更改控件使用的默认系统字体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 11:16