无法在最新 Xcode 版本的 Swift 中设置导航栏字体
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26556583/
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
Cannot Set Navigation Bar Font in Swift on Latest Xcode Version
提问by Benji
The following code was running fine before updating to Xcode 6.1 to stay up with iOS 8.1:
以下代码在更新到 Xcode 6.1 以跟上 iOS 8.1 之前运行良好:
override func viewDidAppear(animated: Bool) {
self.navigationController?.navigationBar.topItem?.title = "Home"
self.navigationController?.navigationBar.titleTextAttributes = [ NSFontAttributeName: UIFont(name: "Lobster 1.4", size: 34), NSForegroundColorAttributeName: UIColor.whiteColor()]
}
The issue is specifically in NSFontAttributeName: UIFont(name: "Lobster 1.4", size: 34)
这个问题具体在 NSFontAttributeName: UIFont(name: "Lobster 1.4", size: 34)
and the error I'm getting here is:
我在这里遇到的错误是:
! "Could not find an overload for 'init' that accepts the supplied arguments"
! "Could not find an overload for 'init' that accepts the supplied arguments"
I found this original code on a different StackOverflow question and it was working as expected until this update (downloaded it yesterday). My font is indeed installed properly.
我在一个不同的 StackOverflow 问题上找到了这个原始代码,它在这个更新之前按预期工作(昨天下载了它)。我的字体确实安装正确。
Should I be writing this code differently now, or is there an entirely new way in which I should set my Navigation Bar Font?
我现在应该以不同的方式编写这段代码,还是有一种全新的方式来设置我的导航栏字体?
Thanks!
谢谢!
回答by Benji
Whoops. I figured this out on my own:
哎呀。我自己想出了这个:
I needed an exclamation point following my declaration of the NSFontAttributeName as it requires a type "NSString!". Perhaps it only required an "NSString" before, but I have no issues now.
我在声明 NSFontAttributeName 之后需要一个感叹号,因为它需要一个类型“NSString!”。也许它以前只需要一个“NSString”,但我现在没有问题。
Working line:
工作线:
NSFontAttributeName: UIFont(name: "Lobster 1.4", size: 24)!
Working full code:
工作完整代码:
override func viewDidAppear(animated: Bool) {
self.navigationController?.navigationBar.topItem?.title = "Home"
self.navigationController?.navigationBar.titleTextAttributes = [ NSFontAttributeName: UIFont(name: "Lobster 1.4", size: 34)!, NSForegroundColorAttributeName: UIColor.whiteColor()]
}
Seems like a silly question now. Hope this helps someone else!
现在似乎是一个愚蠢的问题。希望这对其他人有帮助!
回答by codester
You are using UIFont(name:
intializer as it is defined as
您正在使用UIFont(name:
初始化器,因为它被定义为
init?(name fontName: String, size fontSize: CGFloat) -> UIFont
init?(name fontName: String, size fontSize: CGFloat) -> UIFont
failable intializerread more from link.So it is returning optional.You need to unwrap it as it require AnyObject
not optional.
可失败的初始化器从链接中读取更多信息。因此它返回可选。您需要解开它,因为它不需要AnyObject
可选。
self.navigationController?.navigationBar.titleTextAttributes = [ NSFontAttributeName: UIFont(name: "Lobster 1.4", size: 34)!, NSForegroundColorAttributeName: UIColor.whiteColor()]
回答by Steve Stomp
Thanks Benji!!!
谢谢本吉!!!
I changed it a bit and applied it to the appearance attribute of the navigation controller.
我稍微改动了一下,将它应用到导航控制器的外观属性上。
var navigationBarAppearance = UINavigationBar.appearance()
navigationBarAppearance.titleTextAttributes = [ NSFontAttributeName: UIFont(name: "OpenSans", size: 16)!, NSForegroundColorAttributeName: UIColor.whiteColor()]
回答by IsPha
It is better to declare your font as conditional , like this way :
最好将您的字体声明为有条件的,如下所示:
if let font = UIFont (name: "AdobeArabic-BoldItalic", size: 20) {
UINavigationBar.appearance().titleTextAttributes = [NSFontAttributeName: font]
}
Doing this , makes sure that your font is already found , that I have installed new font , and when used it without conditional if , it issued an exception , of unwrapping an optional .
这样做,可以确保您的字体已经找到,我已经安装了新字体,并且在没有条件的情况下使用它时,它会发出一个异常,打开一个可选的 .
回答by A.G
override func viewWillLayoutSubviews() {
self.navigationController?.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName: UIColor.whiteColor(), NSFontAttributeName : UIFont(name: "Roboto-Regular", size: 14)!]
}
回答by Dary
Swift 4.2
斯威夫特 4.2
Change Small Titlewhen layout scrolled down
更改小标题布局时向下滚动
override func viewDidAppear(animated: Bool) {
self.navigationController?.navigationBar.topItem?.title = "Home"
self.navigationController?.navigationBar.titleTextAttributes = [ NSFontAttributeName: UIFont(name: "Lobster 1.4", size: 34), NSForegroundColorAttributeName: UIColor.whiteColor()]
}
Change Large Title
更改大标题
override func viewDidAppear(animated: Bool) {
self.navigationController?.navigationBar.topItem?.title = "Home"
self.navigationController?.navigationBar.largeTitleTextAttributes = [ NSFontAttributeName: UIFont(name: "Lobster 1.4", size: 34), NSForegroundColorAttributeName: UIColor.whiteColor()]
}