ios 快速更改 titleTextAttribute
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26868847/
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
Changing titleTextAttribute in swift
提问by Peter Pik
Every since i updated xcode i cant seem to change the titleTextAttribute
. Now when i use following code i get this error:
自从我更新 xcode 以来,我似乎无法更改titleTextAttribute
. 现在,当我使用以下代码时,出现此错误:
could not find an overload init that accepts this supplied arguments
找不到接受此提供的参数的重载 init
Code in appDelegate
:
代码appDelegate
:
UINavigationBar.appearance().titleTextAttributes = [NSFontAttributeName: UIFont(name: "Ubuntu", size: 17), NSForegroundColorAttributeName:UIColor.whiteColor()]
UIBarButtonItem.appearance().setTitleTextAttributes([NSFontAttributeName: UIFont(name: "Ubuntu-Light", size: 15), NSForegroundColorAttributeName:UIColor.whiteColor()], forState: UIControlState.Normal)
回答by Nate Cook
There was a recent change so that UIFont(name:size:)
returns an optional UIFont
instance. You'll need to unwrap them to get it to work. Using !
is the easiest way, but will get you a crash if the font isn't on the system. Try something like:
最近发生了变化,因此UIFont(name:size:)
返回了一个可选UIFont
实例。您需要打开它们才能使其正常工作。使用!
是最简单的方法,但如果字体不在系统上,则会导致崩溃。尝试类似:
let navbarFont = UIFont(name: "Ubuntu", size: 17) ?? UIFont.systemFontOfSize(17)
let barbuttonFont = UIFont(name: "Ubuntu-Light", size: 15) ?? UIFont.systemFontOfSize(15)
UINavigationBar.appearance().titleTextAttributes = [NSFontAttributeName: navbarFont, NSForegroundColorAttributeName:UIColor.whiteColor()]
UIBarButtonItem.appearance().setTitleTextAttributes([NSFontAttributeName: barbuttonFont, NSForegroundColorAttributeName:UIColor.whiteColor()], forState: UIControlState.Normal)
回答by Steve Moser
Swift 4:
斯威夫特 4:
UIBarButtonItem.appearance().setTitleTextAttributes([NSAttributedStringKey.foregroundColor: UIColor.white], for: .normal)
UINavigationBar.appearance().titleTextAttributes = [
NSAttributedStringKey.foregroundColor: UIColor.white
]
回答by Andrey Kirsanov
For Swift 3 you could try the following:
对于 Swift 3,您可以尝试以下操作:
UINavigationBar.appearance().titleTextAttributes = [NSForegroundColorAttributeName: UIColor.white]
回答by Satyam
Swift 4:
斯威夫特 4:
if let navFont = UIFont(name: "Futura", size: 18) {
let navBarAttributesDictionary: [NSAttributedStringKey: Any] = [
NSAttributedStringKey(rawValue: NSAttributedStringKey.foregroundColor.rawValue): UIColor(netHex: Colors.BabyBlue.rawValue),
NSAttributedStringKey(rawValue: NSAttributedStringKey.font.rawValue): navFont ]
UINavigationBar.appearance().titleTextAttributes = navBarAttributesDictionary
}
回答by Durul Dalkanat
if let navFont = UIFont(name: "HelveticaNeue-Bold", size: 30.0) {
let navBarAttributesDictionary: [NSObject: AnyObject]? = [
NSForegroundColorAttributeName: UIColor.blackColor(),
NSFontAttributeName: navFont
]
navigationController?.navigationBar.titleTextAttributes = navBarAttributesDictionary
}
回答by Sazzad Hissain Khan
Swift 5
斯威夫特 5
navBar.titleTextAttributes = [NSAttributedString.Key.foregroundColor:titleColor]