How to set font & color of the title in UINavigationBar using iOS5 appearance API?

后端 未结 7 1323
北恋
北恋 2020-12-04 10:50

I have a multiple View Controllers and I want to set the font color of all to red.

 [[UINavigationBar appearance] setFont:[UIFont boldSystemFontOfSize:12.0]]         


        
7条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-04 11:45

    This could be used to set a custom view to a single navigationBar instead of a global setting

    - (void)updateTitleWithString:(NSString *)title
    {
        UIView *headerView = [[UIView alloc] initWithFrame:CGRectZero];
        [headerView setAutoresizingMask:UIViewAutoresizingFlexibleWidth];
        [headerView setAutoresizesSubviews:YES];
    
        CGFloat headFontSize = (IS_SYSTEM_DEVICE_IPAD ? 25.0f : 19.0f);
        UIFont *headFont = [UIFont boldSystemFontOfSize: headFontSize ];
    
        NSMutableParagraphStyle *style = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
        [style setLineBreakMode:NSLineBreakByTruncatingTail];
    
        CGSize size = [title boundingRectWithSize:CGSizeMake(190,headFontSize + 6) options:NSStringDrawingUsesLineFragmentOrigin
                                       attributes:@{NSFontAttributeName : headFont, NSParagraphStyleAttributeName : style} context:nil].size;
    
        headerView.frame  = CGRectMake(0, 0,size.width,self.navigationController.navigationBar.frame.size.height);
        float labelHeight = headFontSize + 6;
        float labelYLoc   = (   self.navigationController.navigationBar.frame.size.height - labelHeight ) / 2;
        UILabel *label    = [[UILabel alloc] initWithFrame:CGRectMake(0,labelYLoc, size.width,labelHeight)];
        label.backgroundColor = [UIColor clearColor];
        label.adjustsFontSizeToFitWidth = YES;
        label.textAlignment = NSTextAlignmentCenter;
        label.textColor = [UIColor whiteColor];
        label.font = headFont;
        label.text = title;
        label.shadowColor = [UIColor colorWithWhite:0.0 alpha:0.4];
        label.lineBreakMode = NSLineBreakByTruncatingTail;
        label.shadowOffset = CGSizeMake(0,-1);
        label.accessibilityLabel = @"

提交回复
热议问题