我通常是一名耐心的调试人员,花费大量时间使用Google搜索并搜索问题的答案。但是,我的耐心已经用完了,我开始相信这根本不可能。
但是,我正在使用导航栏处理iOS应用程序,并选择使用" Optima"我的整个用户界面的字体。我已经能够更改导航栏标题的字体,以及初始视图中的自定义导航栏按钮。但是,在任何后续视图中,我的后退按钮上的字体默认为系统。对于我的生活,我无法找到改变这种状况的方法。
我已经尝试了搜索中出现的所有内容,而我最近的成功来自this blog post。
下面的代码对我有用,但是当然要将我的后退按钮更改为图像,而不是像我希望的那样改变字体。
self.navigationController?.navigationBar.backIndicatorImage = UIImage(named: "Tab_Trophy")
self.navigationController?.navigationBar.backIndicatorTransitionMaskImage = UIImage(named: "Tab_Trophy")
self.navigationItem.backBarButtonItem = UIBarButtonItem(title: "Back", style: UIBarButtonItemStyle.Plain, target: nil, action: nil)
(顺便说一句,上面的代码位于我的后退按钮向用户发送的视图的viewDidLoad()中)
无论如何,我已尝试在不同的地方使用下面的setTitleTextAttributes,但无济于事。
setTitleTextAttributes([NSFontAttributeName: UIFont(name: "Optima", size: 19.0)!], forState: .Normal)
所以我的问题是:可以做到吗?或者我应该使用我想要的字体中的文本制作图像,并使用上面有效的代码吗?
答案 0 :(得分:1)
以下代码将更改顶部' navbar'的字体。和底部的工具栏' items(UINavigationBar.appearance()。titleTextAttributes仅更改导航栏。)
注意:
使用以下方法设置导航栏字体:
navigationController.navigationBar.titleTextAttributes
它通过循环遍历每个工具栏并使用:
来设置工具栏项toolbarItem.setTitleTextAttributes
分享并享受。
var firstShow = false
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
if firstShow {
let navbarAttributes:[String:Any]
let toolbarAttributes:[String:Any]
if let font = UIFont(name: "AmericanTypewriter", size: 22.0) {
navbarAttributes = [
NSFontAttributeName: font,
NSForegroundColorAttributeName : UIColor.init(red:1.0, green:0, blue:0, alpha:1.0)
]
toolbarAttributes = [
NSFontAttributeName: font
]
}
else {
navbarAttributes = [
NSForegroundColorAttributeName : UIColor.init(red:1.0, green:0, blue:0, alpha:1.0)
]
toolbarAttributes = [:]
}
navigationController?.navigationBar.titleTextAttributes = navbarAttributes
if let toolbarItems = navigationController?.toolbar.items {
for toolbarItem in toolbarItems {
toolbarItem.setTitleTextAttributes(toolbarAttributes, for: .normal)
toolbarItem.setTitleTextAttributes(toolbarAttributes, for: .disabled)
}
}
firstShow = false
}
}
答案 1 :(得分:0)
您可以更改应用的所有UIBarButtonItem
标签的字体:
UINavigationBar.appearance().titleTextAttributes = [
NSFontAttributeName: UIFont(name: "Optima", size: 19.0)!
]
或者,如果您只想自定义单个标签,则可以使用以下内容的自定义按钮:
let button = UIButton()
// Customise button as required
let barButtonItem = UIBarButtonItem(customView: button)