ios 自定义字体
by Yuichi Fujiki
藤木雄一
In this article, you'll learn how to create a unified custom look throughout your app with these simple tricks.
在本文中,您将学习如何使用这些简单的技巧在整个应用程序中创建统一的自定义外观。
我们想做什么 (What we want to do)
In iOS, there is a mechanism called UIAppearance
to define an app’s global configuration. For example, you can set a consistent background color of the navigation bar with just one line :
在iOS中,有一种名为UIAppearance
的机制可以定义应用程序的全局配置。 例如,您可以只用一行设置导航栏的一致背景色:
UINavigationBar.appearance().barTntColor = UIColor.blue
However, if you apply the same approach to the font like this:
但是,如果您对字体应用相同的方法,如下所示:
UILabel.appearance().font = UIFont(named: "Gills Sans", size: 14)
all the UILabel
instances will indeed have “Gills Sans”, but with 14pt size as well. I don’t think any app would want to have only 14pt fonts throughout the app. Since UIFont
always needs the size information to be initialized, there is no standard way in UIKit
to just change the typeface without affecting the size.
所有UILabel
实例的确将具有“ Gills Sans”, 但大小也为14pt 。 我认为任何应用程序都不希望整个应用程序中只有 14pt字体。 由于UIFont
始终需要初始化大小信息,因此UIKit
没有标准的方法来更改字体而不影响大小。
But, don’t you want to change the font typeface without hunting down all the Swift files and Interface Builder files? Imagine you have an app with tens of screens and the product owner decides to change the font for the entire app. Or your app wants to cover another language, and you want to use another font for the language because it would look better.
但是,您是否不想在不搜索所有Swift文件和Interface Builder文件的情况下更改字体字体? 假设您有一个带有数十个屏幕的应用程序,产品负责人决定更改整个应用程序的字体。 或者您的应用想要覆盖另一种语言,并且您想要使用另一种字体作为该语言,因为它看起来会更好。
This short article explains how you can do that with just several lines of code.
这篇简短的文章说明了如何仅用几行代码就能做到这一点。
UIView扩展 (UIView Extension)
When you create UIView
extension and declare a property with @objc
modifier, you can set that property through UIAppearance
.
创建UIView
扩展并使用@objc
修饰符声明属性时,可以通过UIAppearance
设置该属性。
For example, if you declare a UILabel
extension property substituteFontName
like this:
例如,如果你声明UILabel
扩展属性substituteFontName
是这样的:
You can call it in AppDelegate.application(:didFinishLaunching...)
您可以在AppDelegate.application(:didFinishLaunching...)
调用它
UILabel.appearance().substituteFontName = "Gills Sans"
And voilà, all UILabel
will be in the “Gills Sans” font with appropriate sizes. ?
而且,所有UILabel
都将使用适当大小的“ Gills Sans”字体。 ?
但是您也想使用粗体字,不是吗? (But you want to use bold font as well, don’t you?)
Life is good so far, but what if you want to specify two different variations of the same font, like bold font? You would think you can just add another extension property substituteBoldFontName
and call it like this, right?
到目前为止,生活还不错,但是如果要指定同一字体的两个不同变体(如粗体字体)怎么办? 您会以为您可以添加另一个扩展属性substituteBoldFontName
并这样称呼,对吗?
UILabel.appearance().substituteFontName = fontNameUILabel.appearance().substituteBoldFontName = boldFontName
Not that easy. If you do this, then all the UILabel
instances show up in bold font. I will explain why.
没那么容易。 如果执行此操作,则所有 UILabel
实例均以粗体显示。 我将解释原因。
It seems that UIAppearance
is just calling all the setter methods of the registered properties in the order they were registered.
似乎UIAppearance
只是按照注册属性的注册顺序调用它们的所有setter方法。
So if we implemented the UILabel
extension like this:
因此,如果我们这样实现UILabel
扩展:
then setting the two properties through UIAppearance
results in the code sequence similar to the following at every UILabel
initialization under the hood.
然后通过UIAppearance
设置这两个属性UIAppearance
导致代码序列类似于在UIAppearance
每次UILabel
初始化时的代码序列。
font = UIFont(name: substituteFontName, size: font.pointSize)font = UIFont(name: substituteBoldFontName, size: font.pointSize)
So, the first line is overwritten by the second line and you are going to have bold fonts everywhere. ?
因此,第一行被第二行覆盖,您到处都会有粗体。 ?
你能为这个做什么? (What can you do about it?)
In order to solve this issue, we can assign proper font style based on the original font style ourselves.
为了解决此问题,我们可以根据自己的原始字体样式分配适当的字体样式。
We can change our UILabel
extension as follows:
我们可以如下更改UILabel
扩展名:
Now, the code sequence that is called at UILabel
initialization will be as follows, and only one of the two font
assignment will be called in a condition.
现在,在UILabel
初始化中调用的代码序列如下,并且在条件中将仅调用两种font
分配中的一种。
if font.fontName.range(of: "Medium") == nil { font = UIFont(name: newValue, size: font.pointSize)}if font.fontName.range(of: "Medium") != nil { font = UIFont(name: newValue, size: font.pointSize)}
As a result, you will have an app with beautifully unified text style while regular and bold fonts co-exist, yay! ?
结果,您将拥有一个具有精美统一文本样式的应用程序,而常规字体和粗体字体并存! ?
You should be able to use the same logic to add more styles like italic fonts as well. Also, you should be able to apply the same approach to another control like UITextField
.
您应该能够使用相同的逻辑来添加更多样式,例如斜体字体。 同样,您应该能够将相同的方法应用于另一个控件,例如UITextField
。
Hope this helps fellow iOS devs, happy coding!!
希望这对其他iOS开发人员有所帮助,祝您编程愉快!!
翻译自: https://www.freecodecamp.org/news/how-to-use-consistent-custom-font-in-an-ios-app-e07b1ddb7a7c/
ios 自定义字体