首页 文章

更改导航栏 Headers 字体 - swift

提问于
浏览
18

我的导航栏中有一个 Headers ,我想将其更改为自定义字体 . 我找到了这行代码,但它适用于你有导航控制器的时候 .

self.navigationController?.navigationBar.titleTextAttributes = [ NSFontAttributeName: UIFont(name: "LeagueGothic-Regular", size: 16.0)!, 
                                                             NSForegroundColorAttributeName: UIColor.whiteColor()]

但我没有任何导航控制器 . 我手动添加导航栏到我的视图 .

enter image description here

enter image description here

我该如何更改评论字体?

5 回答

  • 0

    试试这个:

    Objective-C

    [[UINavigationBar appearance] setTitleTextAttributes:attrsDictionary];
    

    Swift 3

    self.navigationController.navigationBar.titleTextAttributes = [NSFontAttributeName: UIFont(name: "CaviarDreams", size: 20)!]
    

    Swift 4

    self.navigationController.navigationBar.titleTextAttributes = [NSAttributedStringKey.font: UIFont(name: "CaviarDreams", size: 20)!]
    
  • 39

    正确的方法如何为Swift中的每个视图控制器设置字体(使用Appearance代理):

    斯威夫特4

    let attributes = [NSAttributedStringKey.font: UIFont(name: "HelveticaNeue-Light", size: 17)!]
    UINavigationBar.appearance().titleTextAttributes = attributes
    

    斯威夫特3

    let attributes = [NSFontAttributeName: UIFont(name: "HelveticaNeue-Light", size: 17)!]
    UINavigationBar.appearance().titleTextAttributes = attributes
    
  • 19

    SWIFT 4.x

    更改Normal和 Large Title above iOS 11.x 的导航栏 Headers 字体

    let navigation = UINavigationBar.appearance()
    
    let navigationFont = UIFont(name: "Custom_Font_Name", size: 20)
    let navigationLargeFont = UIFont(name: "Custom_Font_Name", size: 34) //34 is Large Title size by default
    
    navigation.titleTextAttributes = [NSAttributedStringKey.foregroundColor: UIColor.white, NSAttributedStringKey.font: navigationFont!]
    
    if #available(iOS 11, *){
        navigation.largeTitleTextAttributes = [NSAttributedStringKey.foregroundColor: UIColor.white, NSAttributedStringKey.font: navigationLargeFont!]
    }
    

    必须在导航栏中将大 Headers 设置为true .

  • -1
    self.navigationController?.navigationBar.titleTextAttributes = [NSAttributedStringKey.font: UIFont(name: "Lato-Semibold", size: 17)!,NSAttributedStringKey.foregroundColor : UIColor.white]
    
  • 4

    要在导航栏的引用上调用titleTextAttributes,请使用:

    let attributes = [NSAttributedStringKey.font: UIFont(name: "Helvetica", size: 17)!]
    self.navigationController?.navigationBar.titleTextAttributes = attributes
    

相关问题