简体   繁体   English

导航栏标题不粗体

[英]Navigation bar title not bold

When I'm in the storyboard, the navigation bar title is shown as bold in the Storyboard as default but when you run the app in simulator and device, it isn't bold. 当我在情节提要中时,导航栏标题在情节提要中默认显示为粗体,但是当您在模拟器和设备中运行应用程序时,它不是粗体。 Annoyingly it isn't possible to change the font size from interface builder. 令人讨厌的是,无法从界面构建器更改字体大小。

Below you can see the difference between what's shown on interface builder and the simulator. 在下面,您可以看到界面生成器和模拟器之间的区别。

Interface builder: 界面生成器: 界面生成器

Simulator: 模拟器: 在此处输入图片说明

I'm aware that I can change the font using something like this: self.navigationController?.navigationBar.titleTextAttributes = [NSFontAttributeName: UIFont(name: "YOUR-FONT-NAME-WEIGHT", size: 24)! 我知道我可以使用以下方式更改字体: self.navigationController?.navigationBar.titleTextAttributes = [NSFontAttributeName: UIFont(name: "YOUR-FONT-NAME-WEIGHT", size: 24)! That would be perfect but I want to keep the font to the default SF font. 那将是完美的,但是我想将字体保持为默认的SF字体。

Is there a workaround this? 有解决方法吗? Any reason why this happens? 有什么原因会发生这种情况?

Use this code in your viewDidLoad to change font size viewDidLoad使用此代码来更改字体大小

self.navigationController?.navigationBar.titleTextAttributes = [
    NSFontAttributeName: UIFont.systemFont(ofSize: 20, weight: UIFontWeightHeavy)
]

For swift 4: 快速4:

self.navigationController?.navigationBar.titleTextAttributes = [
    NSAttributedStringKey.font: UIFont.systemFont(ofSize: 20, weight: UIFont.Weight.heavy)
]

Before in simulator it looks like 在模拟器之前,它看起来像 在此处输入图片说明

And with that code, it looks like 有了这段代码,它看起来像 在此处输入图片说明

只需使用UIFont.systemFont即可获取默认的SF字体。

self.navigationController?.navigationBar.titleTextAttributes = [NSFontAttributeName: UIFont.systemFont(ofSize:24, weight: .bold)]

斯威夫特4:

self.navigationController?.navigationBar.titleTextAttributes = [NSAttributedStringKey.font: UIFont.systemFont(ofSize: 20, weight: UIFont.Weight.heavy)]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM