简体   繁体   English

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

[英]How to set the font size of navigation bar title

I try to set the fontsize of my navigation title and other items. 我尝试设置导航标题和其他项目的fontsize。 How can I do this ? 我怎样才能做到这一点 ?

There are a lot of answers mentoining that we just can set this with the method uinavigationbar.setTitleTextAttributes, but this method doesn't exist (anymore) 我们可以使用uinavigationbar.setTitleTextAttributes方法设置此方法,但是这个方法不存在(不再存在)

Does someone know how to do ? 有人知道该怎么办吗?

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);

Add these lines to your viewDidLoad() method: 将这些行添加到viewDidLoad()方法:

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

try this... 尝试这个...

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

If you are using system font then 如果您正在使用系统字体

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

Update for swift 4 更新swift 4

Presumably you want to set the tittle for UINavigationbar with system fontsize 30 and yellow color. 大概你想用系统fontsize 30和黄色设置UINavigationbar的tittle。 The syntax would probably look like the followings: 语法可能如下所示:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM