如何以编程方式访问iOS系统字体

gog*_*elj 36 cocoa-touch ios swift

我试图更改导航栏标题的字体大小.我知道我可以使用以下方法设置其属性:

var attributes = [ NSForegroundColorAttributeName: UIColor.blackColor(), NSFontAttributeName: UIFont(name: "the font name", size: 18)! ]
Run Code Online (Sandbox Code Playgroud)

...

 self.navigationController?.navigationBar.titleTextAttributes = attributes
Run Code Online (Sandbox Code Playgroud)

我似乎无法找到正确的'系统'字体名称.

我是在默认的,即System,字体名称之后.我尝试打印所有可用的字体只是为了发现它不属于一个家庭,似乎没有一个明确的名称.

Rol*_*som 80

我想你需要:

NSFontAttributeName : UIFont.systemFontOfSize(19.0)
Run Code Online (Sandbox Code Playgroud)

或粗体版:

NSFontAttributeName : UIFont.boldSystemFontOfSize(19.0)
Run Code Online (Sandbox Code Playgroud)

有关用户界面指南和字体的详细信息,请参阅本指南.


Phi*_*ppe 21

您可以像这样访问系统字体,甚至可以设置字体的权重:

  • Swift 3,Swift 4

    UIFont.systemFont(ofSize: 18, weight: UIFontWeightLight)

  • 斯威夫特2

    UIFont.systemFontOfSize(18, weight: UIFontWeightLight)

对于字体粗细,您可以在这些常量之间进行选择,可从iOS 8.2获得:

UIFontWeightUltraLight,
UIFontWeightThin,
UIFontWeightLight,
UIFontWeightRegular,
UIFontWeightMedium,
UIFontWeightSemibold,
UIFontWeightBold,
UIFontWeightHeavy,
UIFontWeightBlack
Run Code Online (Sandbox Code Playgroud)


Mak*_*zev 7

SWIFT 4:较短的版本

UIFont.systemFont(ofSize: 50.0, weight: .regular)
Run Code Online (Sandbox Code Playgroud)


Vin*_*ent 5

(与 Philippe 对最新版本的回答一致)

  • 斯威夫特 4

    UIFont.systemFont(ofSize: 18, weight: UIFont.Weight.light)