【问题标题】:How Properly To Set Font for All Labels in View Controller Using UIAppearance如何使用 UIAppearance 正确设置视图控制器中所有标签的字体
【发布时间】:2016-02-18 09:28:18
【问题描述】:

我正在尝试将整个应用程序中的标签字体(或至少由 View Controller 控制的标签)设置为相同的字体,因此,我选择了创建类别的选项,如推荐的 herehere。出于这些目的,我在 UILabel 类别中创建了方法“setSubstituteFont”。但是,语法

[[UILabel appearance] setSubstituteFont];

如果我将该语句放在我的视图控制器中,总是给我同样的错误:

由于未捕获的异常而终止应用程序 'NSInvalidArgumentException',原因:'-[NSMethodSignature getArgumentTypeAtIndex:]: index (2) out of bounds [0, 1]'

我更喜欢在我的视图控制器中实现该功能,因为我的视图是 .xib,因此,不建议使用“setNeedsDisplay”作为here

我还尝试了其他选项,例如:

[[UILabel appearanceWhenContainedIn:[UIViewController class], nil] setSubstituteFont];

[[UILabel appearanceWhenContainedIn:[self class], nil] setSubstituteFont];

[[UILabel appearanceForTraitCollection:self.traitCollection] setSubstituteFont];

因为根据the documentation,iOS9 中不推荐使用“appearanceWhenContainedIn”,但仍然出现相同的错误,并且应用程序崩溃了。

在两者之间,“setSubstituteFont”方法如下所示:

- (void)setSubstituteFont UI_APPEARANCE_SELECTOR {
    self.font = [UIFont fontWithName:@"GothamRounded-Book" size:54.0];
}

使用的选择器取自the list

【问题讨论】:

    标签: ios objective-c uilabel uiappearance


    【解决方案1】:

    试试这个

    @interface UILabel (UIAPP)
    - (void)setSubstituteFont:(UIFont *)font UI_APPEARANCE_SELECTOR;
    @end
    
    
    @implementation UILabel (UIAPP)
    - (void)setSubstituteFont:(UIFont *)font {
        if (font == nil) {
            self.font = [UIFont fontWithName:@"GothamRounded-Book" size:54.0];
        } else {
            self.font = font;
        }
    }
    @end
    

    即使在视图控制器中也可以在需要的地方调用:

    [[UILabel appearance] setSubstituteFont: nil];
    

    【讨论】:

      猜你喜欢
      • 2013-03-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-04-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多