Hos*_* Ap 38 navigationbar ios swift
我的导航栏中有一个标题,ai想要将其更改为自定义字体.我找到了这行代码,但它适用于你有导航控制器的时候.
self.navigationController?.navigationBar.titleTextAttributes = [ NSFontAttributeName: UIFont(name: "LeagueGothic-Regular", size: 16.0)!,
NSForegroundColorAttributeName: UIColor.whiteColor()]
Run Code Online (Sandbox Code Playgroud)
但我没有任何导航控制器.我手动添加导航栏到我的视图.
我该如何更改评论字体?
KAR*_*KAR 74
试试这个:
Objective-C的
[[UINavigationBar appearance] setTitleTextAttributes:attrsDictionary];
Run Code Online (Sandbox Code Playgroud)
斯威夫特3
self.navigationController.navigationBar.titleTextAttributes = [NSFontAttributeName: UIFont(name: "CaviarDreams", size: 20)!]
Run Code Online (Sandbox Code Playgroud)
斯威夫特4
self.navigationController.navigationBar.titleTextAttributes = [NSAttributedStringKey.font: UIFont(name: "CaviarDreams", size: 20)!]
Run Code Online (Sandbox Code Playgroud)
Mar*_*rný 30
正确的方法如何为Swift中的每个视图控制器设置字体(使用外观代理):
let attributes = [NSAttributedString.Key.font: UIFont(name: "HelveticaNeue-Light", size: 17)!]
UINavigationBar.appearance().titleTextAttributes = attributes
Run Code Online (Sandbox Code Playgroud)
let attributes = [NSAttributedStringKey.font: UIFont(name: "HelveticaNeue-Light", size: 17)!]
UINavigationBar.appearance().titleTextAttributes = attributes
Run Code Online (Sandbox Code Playgroud)
iSr*_*n27 13
SWIFT 4.x
更改iOS 11.x以上的标准和大标题的导航栏标题字体
let navigation = UINavigationBar.appearance()
let navigationFont = UIFont(name: "Custom_Font_Name", size: 20)
let navigationLargeFont = UIFont(name: "Custom_Font_Name", size: 34) //34 is Large Title size by default
navigation.titleTextAttributes = [NSAttributedStringKey.foregroundColor: UIColor.white, NSAttributedStringKey.font: navigationFont!]
if #available(iOS 11, *){
navigation.largeTitleTextAttributes = [NSAttributedStringKey.foregroundColor: UIColor.white, NSAttributedStringKey.font: navigationLargeFont!]
}
Run Code Online (Sandbox Code Playgroud)
必须在导航栏中将大标题设置为true.
Gag*_*hir 11
您也可以在Storyboard中执行此操作,Xcode 10.1中存在一个错误,执行此操作也是解决此问题的技巧。
第1步- 从字体中选择系统
步骤2-然后再次选择Custom(自定义),它将显示所有字体。
我将向您展示我们如何在公司项目中做到这一点。
UINavigationController子类。viewDidLoad.这非常好,原因不止一个。原因之一是中心点的变化。一旦我们改变了类中的某些内容,所有导航控制器都会监听它。
这就是我们的UINavigationController子类的样子。
从工作项目复制粘贴。
import UIKit
class NavigationController: UINavigationController
{
// MARK: Navigation Controller Life Cycle
override func viewDidLoad()
{
super.viewDidLoad()
setFont()
}
// MARK: Methods
func setFont()
{
// set font for title
self.navigationBar.titleTextAttributes = [NSAttributedString.Key.font: UIFont(name: "Al-Jazeera-Arabic", size: 20)!]
// set font for navigation bar buttons
UIBarButtonItem.appearance().setTitleTextAttributes([NSAttributedString.Key.font: UIFont(name: "Al-Jazeera-Arabic", size: 15)!], for: UIControl.State.normal)
}
}
Run Code Online (Sandbox Code Playgroud)
斯威夫特 5 简单的方法
//Programatically
self.navigationController!.navigationBar.titleTextAttributes = [NSAttributedString.Key.font: UIFont(name: "Helvetica Neue", size: 40)!]
Run Code Online (Sandbox Code Playgroud)
身体上
小智 5
navigationController?.navigationBar.titleTextAttributes = [NSAttributedString.Key.font: UIFont(descriptor: UIFontDescriptor(name: "American Typewriter Bold", size: 36), size: 36)]
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
36701 次 |
| 最近记录: |