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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32154426/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 07:21:58  来源:igfitidea点击:

How to access the iOS system font programmatically

iosswiftcocoa-touch

提问by zevij

I am trying to change the font size of the title of a navigation bar. I know I can set its attributes using:

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

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

...

...

 self.navigationController?.navigationBar.titleTextAttributes = attributes

What I cannot seem to find is the correct 'System' font name.

我似乎找不到正确的“系统”字体名称。

I was after the default, a.k.a System, font name. I tried printing all the available fonts only to discover it does not belong to a family and does not seem to have an explicit name.

我追求的是默认的,也就是系统,字体名称。我尝试打印所有可用的字体,结果发现它不属于一个系列,并且似乎没有明确的名称。

回答by Roland Keesom

I think you need:

我认为你需要:

NSFontAttributeName : UIFont.systemFontOfSize(19.0)

Or the bold version:

或者粗体版本:

NSFontAttributeName : UIFont.boldSystemFontOfSize(19.0)

See this guidefor more info on user interface guidelines and fonts.

有关用户界面指南和字体的更多信息,请参阅本指南

回答by Philippe

You can access the system font like this, and even set the weight of the font:

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

  • Swift 3, Swift 4

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

  • Swift 2

    UIFont.systemFontOfSize(18, weight: UIFontWeightLight)

For the font weight you have the choice between those constants, there available from iOS 8.2:

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

UIFontWeightUltraLight,
UIFontWeightThin,
UIFontWeightLight,
UIFontWeightRegular,
UIFontWeightMedium,
UIFontWeightSemibold,
UIFontWeightBold,
UIFontWeightHeavy,
UIFontWeightBlack

回答by Maksim Kniazev

SWIFT 4: shorter version

SWIFT 4:较短的版本

UIFont.systemFont(ofSize: 50.0, weight: .regular)

回答by Vincent

(In line with the answer from Philippe for the latest version)

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

  • Swift 4

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

  • 斯威夫特 4

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

回答by Sujith Chandran

self.navigationController?.navigationBar.titleTextAttributes = [NSFontAttributeName : UIFont.systemFontOfSize(6)]

回答by miche.atucha

Just use methods of UIFont (swift):

只需使用 UIFont (swift) 的方法:

let sysFont: UIFont = UIFont.systemFontOfSize(UIFont.systemFontSize())

Hope it helps!

希望能帮助到你!

回答by Aditi Agrawal

Try the below code:

试试下面的代码:

self.navigationController!.navigationBar.titleTextAttributes = [NSFontAttributeName: UIFont(name:"Arial", size:14.0)!, NSForegroundColorAttributeName:UIColor.blackColor()]

回答by Amirreza

Besides all the answers, it's a better idea to use system font with system styles instead of defining custom sizes and weights. To access them programmatically, for example for the headline, you can use this method:

除了所有答案之外,最好将系统字体与系统样式一起使用,而不是定义自定义大小和粗细。要以编程方式访问它们,例如标题,您可以使用以下方法:

let font = UIFont.preferredFont(forTextStyle: .headline)

I know it is a valid code at least for Swift 5.

我知道至少对于 Swift 5 来​​说这是一个有效的代码。