【问题标题】:How to set the font size of navigation bar title如何设置导航栏标题的字体大小
【发布时间】:2016-01-30 14:37:35
【问题描述】:

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

有很多答案提示我们可以使用方法 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);

【问题讨论】:

    标签: ios swift uinavigationbar font-size


    【解决方案1】:

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

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

    【讨论】:

      【解决方案2】:

      swift 4 更新

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

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

      【讨论】:

        【解决方案3】:

        试试这个...

         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
        

        【讨论】:

          猜你喜欢
          • 2012-03-26
          • 2014-11-20
          • 2016-06-04
          • 2013-10-12
          • 2019-10-28
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多