ios UIFont - 如何获得系统细字体

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31771679/
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:06:34  来源:igfitidea点击:

UIFont - how to get system thin font

iosobjective-cswiftuikituifont

提问by MiguelSlv

UIFonthas methods to get regular font (systemFontOfSize) or bold font (boldSystemFontOfSize), but how to get a "thin system font" available through storyboard?

UIFont有获取常规字体 ( systemFontOfSize) 或粗体 ( boldSystemFontOfSize) 的方法,但是如何通过故事板获取“瘦系统字体”?

Passing "system-thin" to UIFontContructor doesn't work, this constructor only works for non system fonts.

将“系统精简”传递给UIFont构造函数不起作用,此构造函数仅适用于非系统字体。

回答by s1ddok

You can use system font thin weight:

您可以使用系统字体细重:

UIFont.systemFont(ofSize: 34, weight: UIFontWeightThin)

List of available weights for San Francisco:

旧金山可用重量列表:

UIFontWeightUltraLight
UIFontWeightThin
UIFontWeightLight
UIFontWeightRegular
UIFontWeightMedium
UIFontWeightSemibold
UIFontWeightBold
UIFontWeightHeavy
UIFontWeightBlack

san francisco font weights

旧金山字体权重

As of iOS 11,UIFontWeight*was renamed to UIFont.Weight.*. More you can get here https://developer.apple.com/documentation/uikit/uifont.weight.

从 iOS 11 开始,UIFontWeight*更名为UIFont.Weight.*. 您可以在此处获得更多信息https://developer.apple.com/documentation/uikit/uifont.weight

回答by Fahim Parkar

As of iOS 8.2, you can now use UIFont.systemFontOfSize(_ fontSize: CGFloat, weight weight: CGFloat):

从 iOS 8.2 开始,您现在可以使用UIFont.systemFontOfSize(_ fontSize: CGFloat, weight weight: CGFloat)

UIFont.systemFontOfSize(19, weight: UIFontWeightLight)

UIFont.systemFontOfSize(19, weight: UIFontWeightLight)

iOS SDK provided constants for weights:

iOS SDK 提供了权重常量:

UIFontWeightUltraLight
UIFontWeightThin
UIFontWeightLight
UIFontWeightRegular
UIFontWeightMedium
UIFontWeightSemibold
UIFontWeightBold
UIFontWeightHeavy

Using system font is better than creating a font based on font name when you want to use system fonts since iOS can change their system fonts on iOS (like when they did with Helvetica Neue in iOS 7, and now, San Francisco in iOS 9).

当你想使用系统字体时,使用系统字体比基于字体名称创建字体更好,因为 iOS 可以在 iOS 上更改系统字体(就像他们在 iOS 7 中使用 Helvetica Neue 所做的那样,现在,在 iOS 9 中使用 San Francisco) .

So what I would suggest is to include TTF file of the font you want as use that ttf file as custom font and use the custom font in your app.

所以我的建议是包含您想要的字体的 TTF 文件,使用该 ttf 文件作为自定义字体,并在您的应用程序中使用自定义字体。

This is the special reason why I don't like Apple.Nevergo what Apple say. Always do what we want.Apple keep on changingDefault font for every OS.

这就是我不喜欢苹果的特殊原因。永远不要按照苹果说的去做。永远做我们想做的事。Apple 不断更改每个操作系统的默认字体。

回答by GrandFelix

Also if you want to keep same font size and just change weight then use from targeted element font size. For example:

此外,如果您想保持相同的字体大小并仅更改粗细,请使用目标元素字体大小。例如:

demoLabel.font = UIFont.systemFont(ofSize: demoLabel.font.pointSize, weight: UIFontWeightThin)

with this you can keep default label font size and just change weight.

有了这个,你可以保持默认的标签字体大小,只是改变重量。

As of iOS 11,UIFontWeightThin was renamed to UIFont.Weight.thin. More you can get here https://developer.apple.com/documentation/uikit/uifont.weight.

从 iOS 11 开始,UIFontWeightThin 被重命名为UIFont.Weight.thin. 您可以在此处获得更多信息https://developer.apple.com/documentation/uikit/uifont.weight

回答by Pranit

Swift 4.2

斯威夫特 4.2

 label.font = UIFont.systemFont(ofSize: 15, weight: UIFont.Weight.thin)