更改导航栏标题字体 - swift

Hos*_* Ap 38 navigationbar ios swift

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

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

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

在此输入图像描述

在此输入图像描述

我该如何更改评论字体?

KAR*_*KAR 74

试试这个:

Objective-C的

[[UINavigationBar appearance] setTitleTextAttributes:attrsDictionary];
Run Code Online (Sandbox Code Playgroud)

斯威夫特3

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

斯威夫特4

self.navigationController.navigationBar.titleTextAttributes = [NSAttributedStringKey.font: UIFont(name: "CaviarDreams", size: 20)!]
Run Code Online (Sandbox Code Playgroud)

  • 只是为了澄清这个答案,`fontname`应该是UIFont类型,而不只是字体的名称作为字符串。 (2认同)
  • 完全:)虽然它将是"Lato-Light"而不是"Lato-Light.tff":P (2认同)

Mar*_*rný 30

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

斯威夫特4

let attributes = [NSAttributedString.Key.font: UIFont(name: "HelveticaNeue-Light", size: 17)!]
UINavigationBar.appearance().titleTextAttributes = attributes
Run Code Online (Sandbox Code Playgroud)

斯威夫特3

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

  • 对于 iOS 13 及更高版本,请使用“UINavigationBarAppearance()”,请参阅 /sf/answers/4319875311/ (2认同)

iSr*_*n27 13

SWIFT 4.x

更改iOS 11.x以上的标准和大标题的导航栏标题字体

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!]
}
Run Code Online (Sandbox Code Playgroud)

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


Gag*_*hir 11

您也可以在Storyboard中执行此操作,Xcode 10.1中存在一个错误,执行此操作也是解决此问题的技巧。

第1步- 从字体中选择系统

在此处输入图片说明

步骤2-然后再次选择Custom(自定义),它将显示所有字体。

在此处输入图片说明


Ess*_*med 7

雨燕5

我将向您展示我们如何在公司项目中做到这一点。

  1. 我们创建一个UINavigationController子类。
  2. 将我们的定制代码写入viewDidLoad.
  3. 让故事板文件中的每个导航控制器都继承自它。

这非常好,原因不止一个。原因之一是中心点的变化。一旦我们改变了类中的某些内容,所有导航控制器都会监听它。

这就是我们的UINavigationController子类的样子。

从工作项目复制粘贴。

import UIKit

class NavigationController: UINavigationController
{
    // MARK: Navigation Controller Life Cycle

    override func viewDidLoad()
    {
       super.viewDidLoad()
       setFont()
    }

    // MARK: Methods

    func setFont()
    {
       // set font for title
       self.navigationBar.titleTextAttributes = [NSAttributedString.Key.font: UIFont(name: "Al-Jazeera-Arabic", size: 20)!]
    
       // set font for navigation bar buttons
       UIBarButtonItem.appearance().setTitleTextAttributes([NSAttributedString.Key.font: UIFont(name: "Al-Jazeera-Arabic", size: 15)!], for: UIControl.State.normal)
    }
}
Run Code Online (Sandbox Code Playgroud)


Sha*_*med 5

斯威夫特 5 简单的方法

//Programatically
self.navigationController!.navigationBar.titleTextAttributes = [NSAttributedString.Key.font: UIFont(name: "Helvetica Neue", size: 40)!]
Run Code Online (Sandbox Code Playgroud)

身体上

在此输入图像描述

在此输入图像描述

在此输入图像描述

在此输入图像描述


小智 5

雨燕5

navigationController?.navigationBar.titleTextAttributes = [NSAttributedString.Key.font: UIFont(descriptor: UIFontDescriptor(name: "American Typewriter Bold", size: 36), size: 36)]
Run Code Online (Sandbox Code Playgroud)