How can I set custom font in UINavigationBar?

前端 未结 2 1663
隐瞒了意图╮
隐瞒了意图╮ 2021-02-06 10:09

How can I set custom font in UINavigationBar ? I need the tahoma font.

- (void)viewDidLoad{
    self.title =@\"My text\";
}

2条回答
  •  闹比i
    闹比i (楼主)
    2021-02-06 10:52

    This code should work. In uiviewcontroller which presents your main ui:

    - (void)viewDidLoad
    {
            [super viewDidLoad];
    
            int height = navigationController.navigationBar.frame.size.height;
            int width = navigationController.navigationBar.frame.size.width;
    
            UILabel *navLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, width, height)];
            navLabel.backgroundColor = [UIColor clearColor];
            navLabel.textColor = [UIColor whiteColor];
            navLabel.shadowColor = [UIColor colorWithWhite:0.0 alpha:0.5];
            navLabel.font = [UIFont boldSystemFontOfSize:15];
            navLabel.textAlignment = UITextAlignmentCenter;
            self.navigationItem.titleView = navLabel;
            [navLabel release];
    }
    

    Note that resulting custom view has transparent background, so that you can add something more to your navigation bar with [navigationController.navigationBar addSubview:view]. This may be spinner in left corner of the bar or something else.

    If you use custom view, you will not be able set the title with uiviewcontroller title anymore. You need to use the way available by your custom view.

    Example:

    ((UILabel *)self.navigationItem.titleView).text = title;
    

提交回复
热议问题