I am attempting to work around the stupid Apple 'no custom fonts with auto-layout' bug but I am running into issues implementing it in a way that will also work with trait collection updates.
I originally was running the code on a 0.5 delay from a delay-called function on viewDidLoad and in there it was updating the font size and font type correctly for the device. However I learned that traitCollectionDidChange is also called during that time so I moved my code into there instead. Now the font type is not updating but the font size is updating.
Here is my code, it is probably something stupid I am overlooking:
- (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?
}
}
I have managed to figure this out finally. I had to find a way to update once the animation of the transition was finished and I found the way to do that through the UIViewControllerTransitionCoordinator that is passed to willTransitionToTraitCollection. I also have to call my font method in viewDidLoad as well for the initial setup as willTransitionToTraitCollection only calls when a transition is setup and this won't run on app loading and will never run on IPad.
Here is my code now:
- (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;
}
}