ios 如何更改UINavigationBar标题的字体和文字颜色
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31846263/
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
How to change the font and text color of the title of UINavigationBar
提问by Rehaan Advani
I want to be change the font of the title to Avenir, and I want to make it white. Here is my code in the viewDidLoad
method:
我想将标题的字体更改为 Avenir,并且我想让它变白。这是我在viewDidLoad
方法中的代码:
UINavigationBar.appearance().titleTextAttributes = [NSFontAttributeName: UIFont(name: "Avenir", size: 20)!]
UINavigationBar.appearance().titleTextAttributes = [NSForegroundColorAttributeName: UIColor.whiteColor()]
This code only changes the color of the title, not the font. Additionally, I tried implementing this in the App Delegate File, but that did not work.
此代码仅更改标题的颜色,而不更改字体。此外,我尝试在 App Delegate File 中实现这一点,但没有奏效。
Here is what my UINavigationBar looks like:
I want the title to have a font of Avenir as well. How do I achieve this?
我希望标题也有 Avenir 的字体。我如何实现这一目标?
回答by Fred Faust
I use this:
我用这个:
Update, it seems this is needed as well:
更新,似乎也需要这样做:
self.navigationController?.navigationBar.barStyle = UIBarStyle.Black // I then set the color using:
self.navigationController?.navigationBar.barTintColor = UIColor(red: 204/255, green: 47/255, blue: 40/255, alpha: 1.0) // a lovely red
self.navigationController?.navigationBar.tintColor = UIColor.whiteColor() // for titles, buttons, etc.
let navigationTitleFont = UIFont(name: "Avenir", size: 20)!
self.navigationController?.navigationBar.titleTextAttributes = [NSFontAttributeName: navigationTitleFont]
回答by Laky
If you want the styles to be applied through out the whole app add these lines in the AppDelegate.m file.
如果您希望在整个应用程序中应用样式,请在 AppDelegate.m 文件中添加这些行。
NSShadow* shadow = [NSShadow new];
shadow.shadowOffset = CGSizeMake(0.0f, 0.0f);
shadow.shadowColor = [UIColor blackColor];
[[UINavigationBar appearance] setTitleTextAttributes: @{
NSForegroundColorAttributeName: [UIColor whiteColor],
NSFontAttributeName: [UIFont fontWithName:@"Kelvetica Nobis" size:20.0f],
NSShadowAttributeName: shadow
}];
NSForegroundColorAttributeNamesets the Font color.
NSForegroundColorAttributeName设置字体颜色。
NSFontAttributeNamesets a custom font face to the title text.
NSFontAttributeName为标题文本设置自定义字体。
NSShadowAttributeNameapplies a nice shadow effect to the text, you can remove this part if you want.
NSShadowAttributeName为文本应用了一个很好的阴影效果,如果你愿意,你可以删除这部分。
Also in advance, you can add these line to hide the text that comes up in back button in action bar when you navigate to another view within the navigation controller.
同样提前,您可以添加这些行以隐藏当您导航到导航控制器中的另一个视图时操作栏中的后退按钮中出现的文本。
[[UIBarButtonItem appearance]
setBackButtonTitlePositionAdjustment:UIOffsetMake(-1000, -1000)
forBarMetrics:UIBarMetricsDefault];
Hope this help your question :)
希望这对您的问题有所帮助:)
回答by algrid
In case someone needs this in Swift 4:
如果有人在Swift 4 中需要这个:
let navBarAppearance = UINavigationBar.appearance()
navBarAppearance.barTintColor = .red
navBarAppearance.tintColor = .white
navBarAppearance.titleTextAttributes = [
NSAttributedString.Key.foregroundColor: UIColor.white,
NSAttributedString.Key.font: UIFont(name: "Avenir", size: 20)!]
回答by Milad Faridnia
Your code is OK you just need to set all of titleTextAttributes
in one line:
您的代码没问题,您只需要titleTextAttributes
在一行中设置所有内容:
UINavigationBar.appearance().titleTextAttributes = [NSForegroundColorAttributeName: UIColor.white , NSFontAttributeName: UIFont(name: "Avenir", size: 17)!]
回答by Volkodav
I would just create an outlet and do this:
我只想创建一个插座并执行以下操作:
@IBOutlet weak var navigationBar: UINavigationBar!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
navigationBar.titleTextAttributes = [NSFontAttributeName: UIFont(name: "Avenir", size: 30)!]
}
回答by Fabrice Etiennette
In Swift 5:
在Swift 5 中:
let appearance = UINavigationBarAppearance()
appearance.titleTextAttributes = [
NSAttributedString.Key.foregroundColor: UIColor.white,
NSAttributedString.Key.font: UIFont(name: "Avenir", size: 20)!]
appearance.largeTitleTextAttributes = [
NSAttributedString.Key.foregroundColor: UIColor.white,
NSAttributedString.Key.font: UIFont(name: "Avenir", size: 35)!]
Code here are for Title and Large title. you can also remove custom font if you want system font.
这里的代码用于标题和大标题。如果需要系统字体,您还可以删除自定义字体。