[英]Change font size on traitCollectionDidChange
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. 我正在尝试解决愚蠢的Apple“没有带有自动布局的自定义字体”的错误,但是在以与特征集更新一起使用的方式实现该问题时遇到了问题。
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. 我最初是在viewDidLoad上的一个称为延迟的函数上以0.5个延迟运行代码,并在那里为该设备正确更新了字体大小和字体类型。 However I learned that traitCollectionDidChange is also called during that time so I moved my code into there instead.
但是我知道在那段时间也调用了traitCollectionDidChange,所以我将代码移到了那里。 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.
过渡动画完成后,我必须找到一种更新方法,并且找到了一种方法,该方法是通过传递给willTransitionToTraitCollection的UIViewControllerTransitionCoordinator进行的。 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.
对于初始设置,我还必须在viewDidLoad中调用我的字体方法,因为willTransitionToTraitCollection仅在设置了过渡时才调用,这不会在应用程序加载时运行,也永远不会在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;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.