ios 更改导航栏标题字体 - swift
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/39438606/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
change navigation bar title font - swift
提问by Hos Ap
I have a title in my navigation bar and a i want to change it to custom font. I've found this line of code, but it's for when you have a navigation controller.
我的导航栏中有一个标题,我想将其更改为自定义字体。我找到了这行代码,但它适用于您有导航控制器的情况。
self.navigationController?.navigationBar.titleTextAttributes = [ NSFontAttributeName: UIFont(name: "LeagueGothic-Regular", size: 16.0)!,
NSForegroundColorAttributeName: UIColor.whiteColor()]
But i don't have any navigation controller. I added navigation bar manually to my view.
但我没有任何导航控制器。我手动将导航栏添加到我的视图中。
how can i change comment font?
如何更改评论字体?
回答by KAR
Try this:
尝试这个:
Objective-C
目标-C
[[UINavigationBar appearance] setTitleTextAttributes:attrsDictionary];
Swift 3
斯威夫特 3
self.navigationController.navigationBar.titleTextAttributes = [NSFontAttributeName: UIFont(name: "CaviarDreams", size: 20)!]
Swift 4
斯威夫特 4
self.navigationController.navigationBar.titleTextAttributes = [NSAttributedStringKey.font: UIFont(name: "CaviarDreams", size: 20)!]
回答by Marián ?erny
Proper way how to set the font for every view controller in Swift (using Appearance proxy):
如何在 Swift 中为每个视图控制器设置字体的正确方法(使用外观代理):
Swift 5 (and 4.2)
Swift 5(和 4.2)
let attributes = [NSAttributedString.Key.font: UIFont(name: "HelveticaNeue-Light", size: 17)!]
UINavigationBar.appearance().titleTextAttributes = attributes
Swift 4
斯威夫特 4
let attributes = [NSAttributedStringKey.font: UIFont(name: "HelveticaNeue-Light", size: 17)!]
UINavigationBar.appearance().titleTextAttributes = attributes
Swift 3
斯威夫特 3
let attributes = [NSFontAttributeName: UIFont(name: "HelveticaNeue-Light", size: 17)!]
UINavigationBar.appearance().titleTextAttributes = attributes
回答by iSrinivasan27
SWIFT 4.x
斯威夫特 4.x
To Change the Navigation bar title font for both Normal & Large Title above iOS 11.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!]
}
Large Title has to be set true in Navigation bar.
必须在导航栏中将大标题设置为 true。
回答by Gagandeep Gambhir
You can do this in Storyboard as well, there is a bug in Xcode 10.1 of doing this here is a trick to overcome this as well.
您也可以在 Storyboard 中执行此操作,Xcode 10.1 中存在一个错误,此处执行此操作也是克服此问题的一个技巧。
Step 1 - Choose Systemfrom Font
步骤 1 -从字体中选择系统
Step 2 - Then again choose Customand it will show all the fonts.
第 2 步 - 然后再次选择自定义,它将显示所有字体。
回答by Prashant Gaikwad
Swift 4.2 Xcode 10
斯威夫特 4.2 Xcode 10
self.navigationController!.navigationBar.titleTextAttributes = [NSAttributedStringKey.font: UIFont(name: "Sacramento-Regular", size: 19)!]
回答by Essam Mohamed Fahmi
Swift 5
斯威夫特 5
I will show you how we do it in our company projects.
我将向您展示我们如何在公司项目中做到这一点。
- We create a
UINavigationController
subclass. - Write our customization code in
viewDidLoad
. - Make every navigation controller in our storyboard files inherit from it.
- 我们创建一个
UINavigationController
子类。 - 在
viewDidLoad
. - 使故事板文件中的每个导航控制器都继承自它。
That is very good for more than one reason. one of the reasons is the center point of change. Once we change something in the class, all navigation controllers listen to it.
这很好,原因不止一个。原因之一是中心点的变化。一旦我们更改了类中的某些内容,所有导航控制器都会监听它。
That's how our UINavigationController
subclass looks.
这就是我们的UINavigationController
子类的样子。
COPY PASTE from work project.
从工作项目复制粘贴。
import UIKit
class NavigationController: UINavigationController
{
// MARK: View 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)
}
} // end of class
Good luck.
祝你好运。
回答by Davender Verma
self.navigationController?.navigationBar.titleTextAttributes = [NSAttributedStringKey.font: UIFont(name: "Lato-Semibold", size: 17)!,NSAttributedStringKey.foregroundColor : UIColor.white]
回答by Vitalik Kizlov
To call titleTextAttributes on the reference to the navigation bar use:
要在对导航栏的引用上调用 titleTextAttributes,请使用:
let attributes = [NSAttributedStringKey.font: UIFont(name: "Helvetica", size: 17)!]
self.navigationController?.navigationBar.titleTextAttributes = attributes