在traitCollectionDidChange上更改字体大小

时间:2016-06-04 09:09:39

标签: ios objective-c size-classes ios-autolayout

我正在尝试解决愚蠢的Apple'没有自动布局的自定义字体'错误,但我遇到的问题是以一种也适用于特征集合更新的方式实现它。

我最初在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?
    }
}

1 个答案:

答案 0 :(得分:0)

我终于成功解决了这个问题。一旦完成转换动画,我必须找到一种更新方法,并通过传递给willTransitionToTraitCollection的UIViewControllerTransitionCoordinator找到了解决方法。我还必须在viewDidLoad中调用我的font方法以进行初始设置,因为willTransitionToTraitCollection仅在设置转换时调用,并且这不会在app加载时运行,并且永远不会在IPad上运行。

现在是我的代码:

- (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;
    }
}