如何设置导航栏标题的字体大小

How to set the font size of navigation bar title

我尝试设置导航标题和其他项目的字体大小。 我该怎么做?

有很多回答说我们可以用方法 uinavigationbar.setTitleTextAttributes 来设置它,但这个方法不存在(不再存在)

有人知道怎么做吗?

navigationBar = UINavigationBar(frame: CGRectMake(0, 0, fDialogWidth, navbarHeigth))
        let navigationItem = UINavigationItem()
        navigationItem.title = sTitle;
        // Create left and right button for navigation item
        let leftButton = UIBarButtonItem(title: sBacklabel, style: UIBarButtonItemStyle.Plain, target: self, action: "backAction:")
        let rightButton = UIBarButtonItem(title: saddBookmark, style: UIBarButtonItemStyle.Plain, target: self, action: "addBookmarkAction:")



        // Create two buttons for the navigation item
        navigationItem.leftBarButtonItem = leftButton
        navigationItem.rightBarButtonItem = rightButton
        navigationBar.items = [navigationItem];

        navigationBar.frame = CGRectMake(0, 0, screenwidth, navbarHeigth);

试试这个...

 let titleAttributes = [
        NSFontAttributeName: UIFont(name: "your font name", size: 15)!
    ]
 self.navigationController?.navigationBar.titleTextAttributes = titleAttributes

如果你使用的是系统字体那么

 let titleAttributes = [
        NSFontAttributeName: UIFont.systemFontOfSize(15)
 ]
 self.navigationController?.navigationBar.titleTextAttributes = titleAttributes

将这些行添加到您的 viewDidLoad() 方法中:

self.navigationController?.navigationBar.topItem?.title = "Your title" 
self.navigationController?.navigationBar.titleTextAttributes = [ NSFontAttributeName: UIFont(name: "Your Font", size: 34)!, NSForegroundColorAttributeName: UIColor.whiteColor()]

更新 swift 4

您可能想将 UINavigationbar 的标题设置为系统字体大小 30 和黄色。语法可能如下所示:

navigationItem.title = "Your Title"    
navigationController?.navigationBar.titleTextAttributes = [NSAttributedStringKey.font: UIFont.systemFont(ofSize: 30),NSAttributedStringKey.foregroundColor: UIColor.yellow]