我的导航栏中有一个标题,我想将其更改为自定义字体。我找到了这行代码,但它适用于你有导航控制器的时候。
self.navigationController?.navigationBar.titleTextAttributes = [ NSFontAttributeName: UIFont(name: "LeagueGothic-Regular", size: 16.0)!,
NSForegroundColorAttributeName: UIColor.whiteColor()]
但我没有任何导航控制器。我手动添加导航栏到我的视图。
如何更改评论字体?
答案 0 :(得分:56)
试试这个:
<强>目标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)!]
答案 1 :(得分:26)
如何在Swift中设置每个视图控制器的字体(使用Appearance proxy):
let attributes = [NSAttributedStringKey.font: UIFont(name: "HelveticaNeue-Light", size: 17)!]
UINavigationBar.appearance().titleTextAttributes = attributes
let attributes = [NSFontAttributeName: UIFont(name: "HelveticaNeue-Light", size: 17)!]
UINavigationBar.appearance().titleTextAttributes = attributes
答案 2 :(得分:10)
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!]
}
大标题必须在导航栏中设置为真。
答案 3 :(得分:7)
答案 4 :(得分:4)
我将向您展示我们如何在公司项目中做到这一点。
UINavigationController
子类。viewDidLoad
中编写我们的自定义代码。这非常好,原因不止一个。原因之一是变化的中心点。一旦我们在类中更改了某些内容,所有导航控制器便会监听它。
这就是我们的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)
}
}
答案 5 :(得分:3)
navigationController?.navigationBar.titleTextAttributes = [NSAttributedString.Key.font: UIFont(descriptor: UIFontDescriptor(name: "American Typewriter Bold", size: 36), size: 36)]
答案 6 :(得分:2)
Swift 5简单方法
//Programatically
self.navigationController!.navigationBar.titleTextAttributes = [NSAttributedString.Key.font: UIFont(name: "Helvetica Neue", size: 40)!]
物理上
答案 7 :(得分:1)
Swift 4.2 Xcode 10
self.navigationController!.navigationBar.titleTextAttributes = [NSAttributedStringKey.font: UIFont(name: "Sacramento-Regular", size: 19)!]
答案 8 :(得分:0)
self.navigationController?.navigationBar.titleTextAttributes = [NSAttributedStringKey.font: UIFont(name: "Lato-Semibold", size: 17)!,NSAttributedStringKey.foregroundColor : UIColor.white]
答案 9 :(得分:-1)
要在导航栏的引用上调用titleTextAttributes,请使用:
let attributes = [NSAttributedStringKey.font: UIFont(name: "Helvetica", size: 17)!]
self.navigationController?.navigationBar.titleTextAttributes = attributes