ios 自定义字体_如何仅用几行代码在iOS应用中创建一致的自定义字体

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 自定义字体

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值