Change the navigation bar's font

前端 未结 7 1470
没有蜡笔的小新
没有蜡笔的小新 2020-12-02 10:41

The question is plain easy and simple, the answer unfortunately not.

How can you change the font of the text in the UINavigationBar?

7条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-02 11:10

    From iOS 7 and later:

    NSShadow* shadow = [NSShadow new];
    shadow.shadowOffset = CGSizeMake(0.0f, 1.0f);
    shadow.shadowColor = [UIColor redColor];
    [[UINavigationBar appearance] setTitleTextAttributes: @{
         NSForegroundColorAttributeName: [UIColor greenColor],
                    NSFontAttributeName: [UIFont fontWithName:@"Helvetica" size:20.0f],
                  NSShadowAttributeName: shadow
                                                          }];
    

    From iOS 5 and later:

     [[UINavigationBar appearance] setTitleTextAttributes: @{
                                    UITextAttributeTextColor: [UIColor greenColor],
                              UITextAttributeTextShadowColor: [UIColor redColor],
                             UITextAttributeTextShadowOffset: [NSValue valueWithUIOffset:UIOffsetMake(0.0f, 1.0f)],
                                         UITextAttributeFont: [UIFont fontWithName:@"Helvetica" size:20.0f]
         }];
    

    Earlier than iOS 5:

    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 400, 44)];
    label.backgroundColor = [UIColor clearColor];
    label.font = [UIFont boldSystemFontOfSize:20.0];
    label.shadowColor = [UIColor colorWithWhite:0.0 alpha:0.5];
    label.textAlignment = UITextAlignmentCenter;
    label.textColor =[UIColor whiteColor];
    label.text=self.title;  
    self.navigationItem.titleView = label;      
    [label release];
    

提交回复
热议问题