iOS 更改导航栏标题字体和颜色
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19791762/
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
iOS change navigation bar title font and color
提问by aZtraL-EnForceR
So i have this code that should change the nav bar title font, but it doenst
所以我有这个代码应该改变导航栏标题字体,但它确实
NSDictionary *attributes = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont
fontWithName:_dataManager.optionsSettings.fontString size:14], NSFontAttributeName,
[UIColor whiteColor], NSForegroundColorAttributeName, nil];
[[UINavigationBar appearance] setTitleTextAttributes:attributes];
Changing the back button font with this code works just fine.
使用此代码更改后退按钮字体效果很好。
//set backbutton font
NSDictionary *normalAttributes = [NSDictionary dictionaryWithObjectsAndKeys:
[UIFont fontWithName:_dataManager.optionsSettings.fontString size:15], NSFontAttributeName,
nil];
[[UIBarButtonItem appearance] setTitleTextAttributes:normalAttributes
forState:UIControlStateNormal];
回答by Brian
The correct way to change the title font (and color) is:
更改标题字体(和颜色)的正确方法是:
[self.navigationController.navigationBar setTitleTextAttributes:
@{NSForegroundColorAttributeName:[UIColor redColor],
NSFontAttributeName:[UIFont fontWithName:@"mplus-1c-regular" size:21]}];
Edit: Swift 4.2
编辑:斯威夫特 4.2
self.navigationController?.navigationBar.titleTextAttributes =
[NSAttributedString.Key.foregroundColor: UIColor.red,
NSAttributedString.Key.font: UIFont(name: "mplus-1c-regular", size: 21)!]
Edit: Swift 4
编辑:斯威夫特 4
self.navigationController?.navigationBar.titleTextAttributes =
[NSAttributedStringKey.foregroundColor: UIColor.red,
NSAttributedStringKey.font: UIFont(name: "mplus-1c-regular", size: 21)!]
Swift 3:
斯威夫特 3:
self.navigationController?.navigationBar.titleTextAttributes =
[NSForegroundColorAttributeName: UIColor.redColor(),
NSFontAttributeName: UIFont(name: "mplus-1c-regular", size: 21)!]
回答by Tommie C.
There is nothing wrong with the other answers. I'm just sharing the storyboard version for setting the font.
其他答案没有任何问题。我只是分享用于设置字体的故事板版本。
1. Select Your Navigation Bar within your Navigation Controller
1. 在导航控制器中选择导航栏
2. Change the Title Font in the Attributes Inspector
2.在属性检查器中更改标题字体
(You will likely need to toggle the Bar Tint for the Navigation Bar before Xcode picks up the new font)
(在 Xcode 选择新字体之前,您可能需要切换导航栏的 Bar Tint)
Notes (Caveats)
注释(注意事项)
Verified that this does work on Xcode 7.1.1+. (See the Samples below)
验证这确实适用于 Xcode 7.1.1+。(请参阅下面的示例)
- You do need to toggle the nav bar tint before the font takes effect (seems like a bug in Xcode; you can switch it back to default and font will stick)
- If you choose a system font ~ Be sure to make sure the size is not 0.0 (Otherwise the new font will be ignored)
- 您确实需要在字体生效之前切换导航栏色调(似乎是 Xcode 中的一个错误;您可以将其切换回默认值,字体将保持不变)
- 如果选择系统字体~一定要确保大小不是0.0(否则新字体会被忽略)
- Seems like this works with no problem when only one NavBar is in the view hierarchy. It appears that secondary NavBars in the same stack are ignored. (Note that if you show the master navigation controller's navBar all the other custom navBar settings are ignored).
- 当视图层次结构中只有一个 NavBar 时,这似乎没有问题。似乎忽略了同一堆栈中的辅助 NavBar。(请注意,如果您显示主导航控制器的导航栏,则所有其他自定义导航栏设置都将被忽略)。
Gotchas (deux)
陷阱(双人组)
Some of these are repeated which means they are very likely worth noting.
其中一些是重复的,这意味着它们很可能值得注意。
- Sometimes the storyboard xml gets corrupt. This requires you to review the structure in Storyboard as Source Code mode (right click the storyboard file > Open As ...)
- In some cases the navigationItem tag associated with user defined runtime attribute was set as an xml child of the view tag instead of the view controller tag. If so remove it from between the tags for proper operation.
- Toggle the NavBar Tint to ensure the custom font is used.
- Verify the size parameter of the font unless using a dynamic font style
- View hierarchy will override the settings. It appears that one font per stack is possible.
- 有时故事板 xml 会损坏。这需要您在 Storyboard as Source Code 模式中查看结构(右键单击 storyboard 文件 > Open As ...)
- 在某些情况下,与用户定义的运行时属性关联的 navigationItem 标记被设置为视图标记的 xml 子项,而不是视图控制器标记。如果是这样,请将其从标签之间移除以进行正常操作。
- 切换 NavBar Tint 以确保使用自定义字体。
- 验证字体的大小参数,除非使用动态字体样式
- 视图层次结构将覆盖设置。似乎每个堆栈一种字体是可能的。
Result
结果
Samples
样品
- Video Showing Multiple Fonts In Advanced Project
- Simple Source Download
- Advanced Project Download ~ Shows Multiple NavBar Fonts & Custom Font Workaround
- Video Showing Multiple Fonts & Custom Fonts
Handling Custom Fonts
处理自定义字体
Note ~ A nice checklistcan be found from the Code With Chris website and you can see the sample download project.
注意 ~可以从 Code With Chris 网站找到一个不错的清单,您可以查看示例下载项目。
If you have your own font and want to use that in your storyboard, then there is a decent set of answers on the following SO Question. One answer identifies these steps.
如果您有自己的字体并想在故事板中使用它,那么以下SO Question有一组不错的答案。一个答案确定了这些步骤。
- Get you custom font file(.ttf,.ttc)
- Import the font files to your Xcode project
- In the app-info.plist,add a key named Fonts provided by application.It's an array type , add all your font file names to the array,note:including the file extension.
- In the storyboard , on the NavigationBar go to the Attribute Inspector,click the right icon button of the Font select area.In the popup panel , choose Font to Custom, and choose the Family of you embeded font name.
- 获取自定义字体文件(.ttf,.ttc)
- 将字体文件导入您的 Xcode 项目
- 在 app-info.plist 中,添加一个名为 Fonts 由应用程序提供的键。它是一个数组类型,将您所有的字体文件名添加到该数组中,注意:包括文件扩展名。
- 在 Storyboard 中,在 NavigationBar 上转到 Attribute Inspector,单击 Font select 区域的右侧图标按钮。在弹出面板中,选择 Font to Custom,然后选择您嵌入的字体名称的系列。
Custom Font Workaround
自定义字体解决方法
So Xcode naturally looks like it can handle custom fonts on UINavigationItem but that feature is just not updating properly (The font selected is ignored).
因此 Xcode 自然看起来可以处理 UINavigationItem 上的自定义字体,但该功能没有正确更新(忽略所选字体)。
To workaround this:
One way is to fix using the storyboard and adding a line of code: First add a UIView (UIButton, UILabel, or some other UIView subclass) to the View Controller (Not the Navigation Item...Xcode is not currently allowing one to do that). After you add the control you can modify the font in the storyboard and add a reference as an outlet to your View Controller. Just assign that view to the UINavigationItem.titleView. You could also set the text name in code if necessary. Reported Bug (23600285).
要解决此问题:
一种方法是使用故事板修复并添加一行代码:首先将 UIView(UIButton、UILabel 或其他一些 UIView 子类)添加到视图控制器(不是导航项...Xcode 目前不允许这样做那)。添加控件后,您可以修改故事板中的字体并将引用添加为视图控制器的插座。只需将该视图分配给 UINavigationItem.titleView。如有必要,您还可以在代码中设置文本名称。报告错误 (23600285)。
@IBOutlet var customFontTitleView: UIButton!
//Sometime later...
self.navigationItem.titleView = customFontTitleView
回答by Akki
Try this:
尝试这个:
NSDictionary *textAttributes = [NSDictionary dictionaryWithObjectsAndKeys:
[UIColor whiteColor],NSForegroundColorAttributeName,
[UIColor whiteColor],NSBackgroundColorAttributeName,nil];
self.navigationController.navigationBar.titleTextAttributes = textAttributes;
回答by Libor Zapletal
My Swift code for change Navigation Bar title:
我用于更改导航栏标题的 Swift 代码:
let attributes = [NSFontAttributeName : UIFont(name: "Roboto-Medium", size: 16)!, NSForegroundColorAttributeName : UIColor.whiteColor()]
self.navigationController.navigationBar.titleTextAttributes = attributes
And if you want to change background font too then I have this in my AppDelegate:
如果你也想改变背景字体,那么我在我的 AppDelegate 中有这个:
let attributes = [NSFontAttributeName : UIFont(name: "Roboto-Medium", size: 16)!, NSForegroundColorAttributeName : UIColor.whiteColor()]
UIBarButtonItem.appearance().setTitleTextAttributes(attributes, forState: UIControlState.Normal)
回答by Renish Dadhaniya
ADD this single line code in your App Delegate - Did Finish Lauch. It will change Font, color of navigation bar throughout the application.
在您的 App Delegate - Did Finish Launch 中添加此单行代码。它将在整个应用程序中更改导航栏的字体、颜色。
UINavigationBar.appearance().titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.white, NSAttributedString.Key.font: UIFont(name: "YOUR FONT NAME", size: 25.0)!]
回答by Praveen Hiremath
Here is an answer for your question:
以下是您的问题的答案:
Move your code to below method because navigation bar title updated after view loaded. I tried adding above code in viewDidLoad doesn't work, it works fine in viewDidAppear method.
将您的代码移动到下面的方法,因为导航栏标题在视图加载后更新。我尝试在 viewDidLoad 中添加上面的代码不起作用,它在 viewDidAppear 方法中工作正常。
-(void)viewDidAppear:(BOOL)animated{}
回答by Karl Taylor
Anyone needs a Swift 3 version. redColor()
has changed to just red
.
任何人都需要 Swift 3 版本。redColor()
已更改为red
.
self.navigationController?.navigationBar.titleTextAttributes =
[NSForegroundColorAttributeName: UIColor.red,
NSFontAttributeName: UIFont(name: "{your-font-name}", size: 21)!]
回答by O. Boujaouane
I had one problem because when I tried to change my NavigationItem title programmatically i was not able to find my family font (tried many things but impossible to make it run correctly) so I found one workaround very nice and easy in storyboard.
我遇到了一个问题,因为当我尝试以编程方式更改我的 NavigationItem 标题时,我无法找到我的系列字体(尝试了很多东西,但无法使其正确运行),所以我在故事板中找到了一种非常好的和简单的解决方法。
- Firstly you add under Navigation Item one view in middle and don't forget to set backGroundColor to clear color to have the same color of navBar:
- 首先,您在导航项下添加一个视图,不要忘记将 backGroundColor 设置为清除颜色以具有与导航栏相同的颜色:
- Then you add one Label which you can edit (set color of text, font, size...) in this view
- You add constraints to label (Top = 0, Bottom = 0, Trailing = 0 and Leading = 0) to View and center text of label
- 然后在此视图中添加一个可以编辑的标签(设置文本颜色、字体、大小...)
- 您向标签添加约束(顶部 = 0、底部 = 0、尾随 = 0 和前导 = 0)以查看和居中标签文本
Finally you should have something like that in document outline:
最后,您应该在文档大纲中有类似的内容:
And something like that in your ViewController:
在你的 ViewController 中类似的东西:
Hope it can help.
希望它能有所帮助。
回答by sPoz
It's a bit more readable using literals:
使用文字更易读:
self.navigationController.navigationBar.titleTextAttributes = @{
NSFontAttributeName:[UIFont fontWithName:@"mplus-1c-regular" size:21],
NSForegroundColorAttributeName: [UIColor whiteColor]
};
回答by karthik
Swift:-
迅速:-
self.navigationController?.navigationBar.titleTextAttributes = [NSAttributedStringKey.foregroundColor: UIColor.white, NSAttributedStringKey.font: UIFont(name:"Love Nature", size: 40)!]