【发布时间】:2016-02-18 09:28:18
【问题描述】:
我正在尝试将整个应用程序中的标签字体(或至少由 View Controller 控制的标签)设置为相同的字体,因此,我选择了创建类别的选项,如推荐的 here 或 here。出于这些目的,我在 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