【发布时间】:2015-08-13 22:36:22
【问题描述】:
我到处寻找,似乎找不到如何更改导航栏后退按钮文本的粗细。我希望它更薄。
另外,当我们谈论厚度时,你会如何改变导航栏标题的厚度?
谢谢!
【问题讨论】:
标签: ios xcode swift ios8 xcode6
我到处寻找,似乎找不到如何更改导航栏后退按钮文本的粗细。我希望它更薄。
另外,当我们谈论厚度时,你会如何改变导航栏标题的厚度?
谢谢!
【问题讨论】:
标签: ios xcode swift ios8 xcode6
您可以通过调整导航栏上的titleTextAttributes和后退按钮项来修改字体样式。
在 iOS 版本 8.2 之前,调整字体粗细的唯一方法是选择其变体之一,例如 HelveticaNeue-Light 或 HelveticaNeue-Bold 或使用
UIFont.boldSystemFontOfSize(_ fontSize: CGFloat)
但是,没有等效的 lightSystemFontOfSize 工厂函数。要减少后退按钮和标题文本的重量和大小,您可以使用类似这样的内容(在呈现视图控制器内)
let rootController = UIViewController()
rootController.navigationItem.backBarButtonItem = UIBarButtonItem(title: "Back",
style: .Plain, target: nil, action: nil)
rootController.navigationItem.backBarButtonItem?.setTitleTextAttributes(
[NSFontAttributeName: UIFont(name: "HelveticaNeue-Light", size: 15)!],
forState: .Normal)
let detailController = UIViewController()
detailController.title = "Title"
let navController = UINavigationController()
navController.viewControllers = [rootController, detailController]
navController.navigationBar.titleTextAttributes = [
NSFontAttributeName: UIFont(name: "HelveticaNeue", size: 18)!]
presentViewController(navController, animated: true, completion: nil)
产生以下样式
从 iOS 8.2 开始,您可以使用权重属性初始化系统字体。
UIFont.systemFontOfSize(_ fontSize: CGFloat, weight weight: CGFloat)
在Apple's official documentation 中可以找到一组预定义的权重常数。
在前面的例子的基础上,你可以做这样的事情
let rootController = UIViewController()
rootController.navigationItem.backBarButtonItem = UIBarButtonItem(title: "Back",
style: .Plain, target: nil, action: nil)
rootController.navigationItem.backBarButtonItem?.setTitleTextAttributes(
[NSFontAttributeName: UIFont.systemFontOfSize(15, weight: UIFontWeightThin)],
forState: .Normal)
let detailController = UIViewController()
detailController.title = "Title"
let navController = UINavigationController()
navController.viewControllers = [rootController, detailController]
navController.navigationBar.titleTextAttributes = [
NSFontAttributeName: UIFont.systemFontOfSize(18, weight: UIFontWeightLight)]
最终形成这样的风格
【讨论】: