【问题标题】:Change font size on traitCollectionDidChange在 traitCollectionDidChange 上更改字体大小
【发布时间】:2016-06-04 09:09:38
【问题描述】:

我正在尝试解决愚蠢的 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?
    }
}

【问题讨论】:

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


    【解决方案1】:

    我终于想通了。一旦过渡动画完成,我必须找到一种更新方法,并且我通过传递给 willTransitionToTraitCollection 的 UIViewControllerTransitionCoordinator 找到了更新方法。我还必须在 viewDidLoad 中调用我的字体方法以及初始设置,因为 willTransitionToTraitCollection 仅在设置转换时调用,并且不会在应用加载时运行,也永远不会在 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;
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2014-02-18
      • 2018-12-09
      • 2016-06-05
      • 2020-05-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-01-19
      • 2011-02-22
      相关资源
      最近更新 更多