更改 traitCollectionDidChange 上的字体大小
Change font size on traitCollectionDidChange
我正在尝试解决愚蠢的 Apple 'no custom fonts with auto-layout' 错误,但我 运行 遇到了以同样适用于特征集合更新的方式实施它的问题。
我最初是 运行 viewDidLoad 上的延迟调用函数延迟 0.5 的代码,它正在为设备正确更新字体大小和字体类型。但是我了解到 traitCollectionDidChange 在那段时间也被调用,所以我将我的代码移到了那里。现在字体类型没有更新,但字体大小正在更新。
这是我的代码,我可能忽略了一些愚蠢的东西:
- (void)traitCollectionDidChange:(UITraitCollection *)previousTraitCollection{
//[super traitCollectionDidChange: previousTraitCollection];
//Create horz/vert vars for easier iffing
UIUserInterfaceSizeClass viewHorizontal = self.view.traitCollection.horizontalSizeClass;
UIUserInterfaceSizeClass viewVertical = self.view.traitCollection.verticalSizeClass;
//Change font and font size depending on classes; really just the size but we need to set the font anyway
if (viewHorizontal == UIUserInterfaceSizeClassCompact && viewVertical == UIUserInterfaceSizeClassCompact){
agTtl_Text.font = [UIFont fontWithName:@"Hobo Std" size:36];
agSubttl_Text.font = [UIFont fontWithName:@"Hobo Std" size:20];
actBtn.titleLabel.font = [UIFont fontWithName:@"Hobo Std" size:20];
nAccBtn.titleLabel.font = [UIFont fontWithName:@"Hobo Std" size:20];
} else if (viewHorizontal == UIUserInterfaceSizeClassCompact && viewVertical == UIUserInterfaceSizeClassRegular){
agTtl_Text.editable = YES; //still need this iOS 6 hack?
agTtl_Text.font = [UIFont fontWithName:@"Hobo Std" size:35];
agSubttl_Text.font = [UIFont fontWithName:@"Hobo Std" size:20];
actBtn.titleLabel.font = [UIFont fontWithName:@"Hobo Std" size:30];
nAccBtn.titleLabel.font = [UIFont fontWithName:@"Hobo Std" size:30];
agTtl_Text.editable = NO; //still need this iOS 6 hack?
}
}
我终于弄明白了。我必须找到一种方法来在过渡动画完成后进行更新,并且我找到了通过传递给 willTransitionToTraitCollection 的 UIViewControllerTransitionCoordinator 来执行此操作的方法。我还必须在 viewDidLoad 中调用我的字体方法以及初始设置,因为 willTransitionToTraitCollection 仅在设置转换时调用,这不会 运行 应用程序加载并且永远不会 运行 [=15] =].
这是我的代码:
- (void)viewDidLoad
{
[self performSelector:@selector(setFontSettings) withObject:nil afterDelay:0.5];
rtnMsg = [[NSMutableArray alloc] init];
db = [[Database alloc] init];
rtnMsg = [db bDoesDBExist];
if ([rtnMsg[0] boolValue] == NO){
[actBtn setTitle:@"Tap Here to Begin" forState:UIControlStateNormal];
bDBExists = NO;
} else{
//status.text = @"Found database";
[actBtn setTitle:@"Tap Here to Login" forState:UIControlStateNormal];
nAccBtn.hidden = NO;
nAccBtn.enabled = YES;
bDBExists = YES;
//self.navigationItem.title=@"New Patient Profile Creator";
}
[super viewDidLoad];
}
- (void)willTransitionToTraitCollection:(UITraitCollection *)newCollection withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator{
[coordinator animateAlongsideTransition:nil completion:^(id<UIViewControllerTransitionCoordinatorContext> context) {
[self setFontSettings];
}];
}
- (void)setFontSettings{
//[super traitCollectionDidChange: previousTraitCollection];
//Create horz/vert vars for easier iffing
UIUserInterfaceSizeClass viewHorizontal = self.view.traitCollection.horizontalSizeClass;
UIUserInterfaceSizeClass viewVertical = self.view.traitCollection.verticalSizeClass;
//Change font and font size depending on classes; really just the size but we need to set the font anyway
if (viewHorizontal == UIUserInterfaceSizeClassCompact && viewVertical == UIUserInterfaceSizeClassCompact){
agTtl_Text.font = [UIFont fontWithName:@"Hobo Std" size:36];
agSubttl_Text.font = [UIFont fontWithName:@"Hobo Std" size:20];
actBtn.titleLabel.font = [UIFont fontWithName:@"Hobo Std" size:20];
nAccBtn.titleLabel.font = [UIFont fontWithName:@"Hobo Std" size:20];
} else if (viewHorizontal == UIUserInterfaceSizeClassCompact && viewVertical == UIUserInterfaceSizeClassRegular){
agTtl_Text.editable = YES;
agTtl_Text.font = [UIFont fontWithName:@"Hobo Std" size:35];
agSubttl_Text.font = [UIFont fontWithName:@"Hobo Std" size:20];
actBtn.titleLabel.font = [UIFont fontWithName:@"Hobo Std" size:30];
nAccBtn.titleLabel.font = [UIFont fontWithName:@"Hobo Std" size:30];
agTtl_Text.editable = NO;
} else if ((viewHorizontal == UIUserInterfaceSizeClassRegular && viewVertical == UIUserInterfaceSizeClassRegular) ||
(viewHorizontal == UIUserInterfaceSizeClassRegular && viewVertical == UIUserInterfaceSizeClassCompact)){
agTtl_Text.editable = YES;
agTtl_Text.font = [UIFont fontWithName:@"Hobo Std" size:50];
agSubttl_Text.font = [UIFont fontWithName:@"Hobo Std" size:34];
actBtn.titleLabel.font = [UIFont fontWithName:@"Hobo Std" size:30];
nAccBtn.titleLabel.font = [UIFont fontWithName:@"Hobo Std" size:30];
agTtl_Text.editable = NO;
}
}
我正在尝试解决愚蠢的 Apple 'no custom fonts with auto-layout' 错误,但我 运行 遇到了以同样适用于特征集合更新的方式实施它的问题。
我最初是 运行 viewDidLoad 上的延迟调用函数延迟 0.5 的代码,它正在为设备正确更新字体大小和字体类型。但是我了解到 traitCollectionDidChange 在那段时间也被调用,所以我将我的代码移到了那里。现在字体类型没有更新,但字体大小正在更新。
这是我的代码,我可能忽略了一些愚蠢的东西:
- (void)traitCollectionDidChange:(UITraitCollection *)previousTraitCollection{
//[super traitCollectionDidChange: previousTraitCollection];
//Create horz/vert vars for easier iffing
UIUserInterfaceSizeClass viewHorizontal = self.view.traitCollection.horizontalSizeClass;
UIUserInterfaceSizeClass viewVertical = self.view.traitCollection.verticalSizeClass;
//Change font and font size depending on classes; really just the size but we need to set the font anyway
if (viewHorizontal == UIUserInterfaceSizeClassCompact && viewVertical == UIUserInterfaceSizeClassCompact){
agTtl_Text.font = [UIFont fontWithName:@"Hobo Std" size:36];
agSubttl_Text.font = [UIFont fontWithName:@"Hobo Std" size:20];
actBtn.titleLabel.font = [UIFont fontWithName:@"Hobo Std" size:20];
nAccBtn.titleLabel.font = [UIFont fontWithName:@"Hobo Std" size:20];
} else if (viewHorizontal == UIUserInterfaceSizeClassCompact && viewVertical == UIUserInterfaceSizeClassRegular){
agTtl_Text.editable = YES; //still need this iOS 6 hack?
agTtl_Text.font = [UIFont fontWithName:@"Hobo Std" size:35];
agSubttl_Text.font = [UIFont fontWithName:@"Hobo Std" size:20];
actBtn.titleLabel.font = [UIFont fontWithName:@"Hobo Std" size:30];
nAccBtn.titleLabel.font = [UIFont fontWithName:@"Hobo Std" size:30];
agTtl_Text.editable = NO; //still need this iOS 6 hack?
}
}
我终于弄明白了。我必须找到一种方法来在过渡动画完成后进行更新,并且我找到了通过传递给 willTransitionToTraitCollection 的 UIViewControllerTransitionCoordinator 来执行此操作的方法。我还必须在 viewDidLoad 中调用我的字体方法以及初始设置,因为 willTransitionToTraitCollection 仅在设置转换时调用,这不会 运行 应用程序加载并且永远不会 运行 [=15] =].
这是我的代码:
- (void)viewDidLoad
{
[self performSelector:@selector(setFontSettings) withObject:nil afterDelay:0.5];
rtnMsg = [[NSMutableArray alloc] init];
db = [[Database alloc] init];
rtnMsg = [db bDoesDBExist];
if ([rtnMsg[0] boolValue] == NO){
[actBtn setTitle:@"Tap Here to Begin" forState:UIControlStateNormal];
bDBExists = NO;
} else{
//status.text = @"Found database";
[actBtn setTitle:@"Tap Here to Login" forState:UIControlStateNormal];
nAccBtn.hidden = NO;
nAccBtn.enabled = YES;
bDBExists = YES;
//self.navigationItem.title=@"New Patient Profile Creator";
}
[super viewDidLoad];
}
- (void)willTransitionToTraitCollection:(UITraitCollection *)newCollection withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator{
[coordinator animateAlongsideTransition:nil completion:^(id<UIViewControllerTransitionCoordinatorContext> context) {
[self setFontSettings];
}];
}
- (void)setFontSettings{
//[super traitCollectionDidChange: previousTraitCollection];
//Create horz/vert vars for easier iffing
UIUserInterfaceSizeClass viewHorizontal = self.view.traitCollection.horizontalSizeClass;
UIUserInterfaceSizeClass viewVertical = self.view.traitCollection.verticalSizeClass;
//Change font and font size depending on classes; really just the size but we need to set the font anyway
if (viewHorizontal == UIUserInterfaceSizeClassCompact && viewVertical == UIUserInterfaceSizeClassCompact){
agTtl_Text.font = [UIFont fontWithName:@"Hobo Std" size:36];
agSubttl_Text.font = [UIFont fontWithName:@"Hobo Std" size:20];
actBtn.titleLabel.font = [UIFont fontWithName:@"Hobo Std" size:20];
nAccBtn.titleLabel.font = [UIFont fontWithName:@"Hobo Std" size:20];
} else if (viewHorizontal == UIUserInterfaceSizeClassCompact && viewVertical == UIUserInterfaceSizeClassRegular){
agTtl_Text.editable = YES;
agTtl_Text.font = [UIFont fontWithName:@"Hobo Std" size:35];
agSubttl_Text.font = [UIFont fontWithName:@"Hobo Std" size:20];
actBtn.titleLabel.font = [UIFont fontWithName:@"Hobo Std" size:30];
nAccBtn.titleLabel.font = [UIFont fontWithName:@"Hobo Std" size:30];
agTtl_Text.editable = NO;
} else if ((viewHorizontal == UIUserInterfaceSizeClassRegular && viewVertical == UIUserInterfaceSizeClassRegular) ||
(viewHorizontal == UIUserInterfaceSizeClassRegular && viewVertical == UIUserInterfaceSizeClassCompact)){
agTtl_Text.editable = YES;
agTtl_Text.font = [UIFont fontWithName:@"Hobo Std" size:50];
agSubttl_Text.font = [UIFont fontWithName:@"Hobo Std" size:34];
actBtn.titleLabel.font = [UIFont fontWithName:@"Hobo Std" size:30];
nAccBtn.titleLabel.font = [UIFont fontWithName:@"Hobo Std" size:30];
agTtl_Text.editable = NO;
}
}