34

我正在尝试在整个iOS app. 我发现我可以使用:

 [[UILabel appearance] setFont:[UIFont fontWithName:@"Proxima Nova" size:17.0]];
  • 为所有UILabels. 但是,并非所有人都UILabels共享相同的字体大小。

  • 为整个 iOS 应用程序设置默认字体?,有人有同样的担忧,并被告知将 size 参数设置为 0.0 以仅设置字体而不设置字体大小。

  • 当我尝试这样做时,UILabel我的应用程序中的所有文本都消失了(因为显然iOS字面意思是 0.0 字体大小)。

关于如何普遍设置字体而不是大小的任何建议?非常感谢!

4

7 回答 7

24
- (void)viewDidLoad
{
    [super viewDidLoad];
    [self setFontFamily:@"FagoOfficeSans-Regular" forView:self.view andSubViews:YES];
}

-(void)setFontFamily:(NSString*)fontFamily forView:(UIView*)view andSubViews:(BOOL)isSubViews
{
    if ([view isKindOfClass:[UILabel class]])
    {
        UILabel *lbl = (UILabel *)view;
        [lbl setFont:[UIFont fontWithName:fontFamily size:[[lbl font] pointSize]]];
    }

    if (isSubViews)
    {
        for (UIView *sview in view.subviews)
        {
            [self setFontFamily:fontFamily forView:sview andSubViews:YES];
        }
    }    
}
于 2012-08-27T10:10:13.623 回答
19

这是 Objective-C 中的一个解决方案,将此类别放在您想要更改的任何位置UILabel Apperance而不设置UILabel FontSize

@implementation UILabel (SubstituteFontName)
- (void)setSubstituteFontName:(NSString *)name UI_APPEARANCE_SELECTOR {
    self.font = [UIFont fontWithName:name size:self.font.pointSize];
}
@end

然后,您可以更改Apperance

[[UILabel appearance] setSubstituteFontName:@"SourceSansPro-Light"];
于 2015-04-16T13:53:50.107 回答
12

我在我的项目中使用了公认的答案,但需要一个更通用的功能,因此它将字体更改为每种可能的字体,我还选择在一些库存字体与我们的自定义字体之间设置映射,所以它们'也可以通过storybuilder和xib文件访问。

+ (void)setupFontsForView:(UIView *)view andSubViews:(BOOL)isSubViews
{
    if ([view respondsToSelector:@selector(setFont:)] && [view respondsToSelector:@selector(font)]) {
        id      viewObj = view;
        UIFont  *font   = [viewObj font];

        if ([font.fontName isEqualToString:@"AcademyEngravedLetPlain"]) {
            [viewObj setFont:[UIFont fontWithName:PRIMARY_FONT size:font.pointSize]];
        } else if ([font.fontName hasPrefix:@"AmericanTypewriter"]) {
            [viewObj setFont:[UIFont fontWithName:SECONDARY_FONT size:font.pointSize]];
        }
    }

    if (isSubViews) {
        for (UIView *sview in view.subviews) {
            [self setupFontsForView:sview andSubViews:YES];
        }
    }
}
于 2013-04-30T20:50:34.130 回答
6

通过为 Label 编写类别,我们可以更改整个应用程序的字体。

@implementation UILabel (CustomeFont)

-(void)awakeFromNib
{
    [super awakeFromNib];
    [self setBackgroundColor:[UIColor clearColor]];
    [self setFont:[UIFont fontWithName:@"Helvetica" size:self.font.pointSize]];
}

@end
于 2013-09-10T14:48:45.783 回答
5

斯威夫特3 https://gist.github.com/dimohamdy/c47992e14c1a3ee3315c06e2480e121e

您无法为标签字体设置外观,这使我创建了默认字体的其他值,当我设置新字体时,我只获得新字体的名称和标签的旧大小并创建其他字体并设置此字体

    //when you want to set font for all labels in Application
    UILabel.appearance().defaultFont =  UIFont.systemFont(ofSize: 15/*Any Value*/, weight: UIFontWeightThin)

extension UILabel{
    dynamic var defaultFont: UIFont? {
        get { return self.font }
        set {
          //get old size of lable font
          let sizeOfOldFont = self.font.pointSize 
          //get new name  of font
          let fontNameOfNewFont = newValue?.fontName
          self.font = UIFont(name: fontNameOfNewFont!, size: sizeOfOldFont)
        }
    }
     }
于 2016-09-28T15:03:59.737 回答
3

UIView+DefaultFontAndColor.h

#import <UIKit/UIKit.h>

@interface UIView (DefaultFontAndColor)

-(void)setDefaultFontFamily:(NSString*)fontFamily andSubViews:(BOOL)isSubViews andColor:(UIColor*) color;
@end

UIView+DefaultFontAndColor.m

#import "UIView+DefaultFontAndColor.h"

@implementation UIView (DefaultFontAndColor)

//sets the default font for view classes by default
-(void)setDefaultFontFamily:(NSString*)fontFamily andSubViews:(BOOL)isSubViews andColor:     (UIColor*) color
{
    if ([self isKindOfClass:[UILabel class]])
    {
        UILabel *lbl = (UILabel *)self;
        [lbl setFont:[UIFont fontWithName:fontFamily size:[[lbl font] pointSize]]];

        if( color )
            lbl.textColor = color;
    }
    else if ([self isKindOfClass:[UIButton class]])
    {
        UIButton *btn = (UIButton *)self;
        [btn.titleLabel setFont:[UIFont fontWithName:fontFamily size:[[btn.titleLabel font] pointSize]]];

        if( color )
        {
            btn.tintColor = color;
        }
    }

    if (isSubViews)
    {
        for (UIView *sview in self.subviews)
        {
            [sview setDefaultFontFamily:fontFamily andSubViews:YES andColor:color];
        }
    }
}
@end

@usage:没有颜色:

#import "UIView+DefaultFontAndColor.h"

UIView myView = [[UIView alloc] init]
[myView setDefaultFontFamily:@"Arial" andSubViews:YES andColor:nil];

@用法:颜色:

#import "UIView+DefaultFontAndColor.h"

UIView myView = [[UIView alloc] init]
[myView setDefaultFontFamily:@"Arial" andSubViews:YES andColor:[UIColor greenColor] ];
于 2014-02-15T00:54:24.977 回答
3

Raegtime 的回答 ala Swift ......

import UIKit

extension UIViewController {
    func setFontFamilyForView(_ fontFamily: String, view: UIView, andSubviews: Bool) {
        if let label = view as? UILabel {
            label.font = UIFont(name: fontFamily, size: label.font.pointSize)
        }

        if let textView = view as? UITextView {
            textView.font = UIFont(name: fontFamily, size: textView.font!.pointSize)
        }

        if let textField = view as? UITextField {
            textField.font = UIFont(name: fontFamily, size: textField.font!.pointSize)
        }

        if andSubviews {
            for v in view.subviews {
                setFontFamilyForView(fontFamily, view: v, andSubviews: true)
            }
        }
    }
}
于 2018-02-24T15:54:46.103 回答