我正在尝试更改导航栏标题的字体大小。我知道我可以使用以下方法设置其属性:
var attributes = [ NSForegroundColorAttributeName: UIColor.blackColor(), NSFontAttributeName: UIFont(name: "the font name", size: 18)! ]
...
self.navigationController?.navigationBar.titleTextAttributes = attributes
我似乎无法找到正确的'系统'字体名称。
我是在默认的a.k.a系统,字体名称之后。我尝试打印所有可用的字体只是为了发现它不属于一个家庭,似乎没有明确的名称。
答案 0 :(得分:77)
我认为你需要:
NSFontAttributeName : UIFont.systemFontOfSize(19.0)
或粗体版:
NSFontAttributeName : UIFont.boldSystemFontOfSize(19.0)
有关用户界面指南和字体的详细信息,请参阅this guide。
答案 1 :(得分:18)
您可以像这样访问系统字体,甚至可以设置字体的重量:
UIFont.systemFont(ofSize: 18, weight: UIFontWeightLight)
UIFont.systemFontOfSize(18, weight: UIFontWeightLight)
对于字体粗细,您可以在这些常量之间进行选择,可以从iOS 8.2获得:
UIFontWeightUltraLight,
UIFontWeightThin,
UIFontWeightLight,
UIFontWeightRegular,
UIFontWeightMedium,
UIFontWeightSemibold,
UIFontWeightBold,
UIFontWeightHeavy,
UIFontWeightBlack
答案 2 :(得分:4)
SWIFT 4: 较短的版本
UIFont.systemFont(ofSize: 50.0, weight: .regular)
答案 3 :(得分:2)
self.navigationController?.navigationBar.titleTextAttributes = [NSFontAttributeName : UIFont.systemFontOfSize(6)]
答案 4 :(得分:2)
只需使用UIFont(swift)的方法:
let sysFont: UIFont = UIFont.systemFontOfSize(UIFont.systemFontSize())
希望它有所帮助!
答案 5 :(得分:2)
(根据Philippe对最新版本的回答)
Swift 4
UIFont.systemFont(ofSize: 18, weight: UIFont.Weight.light)
答案 6 :(得分:2)
除了所有答案外,最好将系统字体与系统样式一起使用,而不是定义自定义大小和粗细。要以编程方式访问它们(例如标题),可以使用以下方法:
let font = UIFont.preferredFont(forTextStyle: .headline)
我知道这至少对于Swift 5是有效的代码。
答案 7 :(得分:0)
请尝试以下代码:
self.navigationController!.navigationBar.titleTextAttributes = [NSFontAttributeName: UIFont(name:"Arial", size:14.0)!, NSForegroundColorAttributeName:UIColor.blackColor()]