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

6

我想设置导航标题和其他项目的字体大小。如何做到这一点?

很多答案提到我们只需使用方法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);
3个回答

18

将以下代码添加到您的viewDidLoad()方法中:

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

3

Swift 4更新

假设您希望使用系统字体大小30和黄色颜色为UINavigationBar设置标题。语法可能如下所示:

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

2

尝试这个...

 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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接