博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
ios 自定义字体_如何仅用几行代码在iOS应用中创建一致的自定义字体
阅读量:2518 次
发布时间:2019-05-11

本文共 4864 字,大约阅读时间需要 16 分钟。

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开发人员有所帮助,祝您编程愉快!!

翻译自:

ios 自定义字体

转载地址:http://oerwd.baihongyu.com/

你可能感兴趣的文章
Nginx
查看>>
Navicat远程连接云主机数据库
查看>>
Nginx配置文件nginx.conf中文详解(总结)
查看>>
jxl写入excel实现数据导出功能
查看>>
linux文件目录类命令|--cp指令
查看>>
.net MVC 404错误解决方法
查看>>
linux系统目录结构
查看>>
Entity Framework 4.3.1 级联删除
查看>>
学习进度
查看>>
github.com加速节点
查看>>
使用Postmark测试后端存储性能
查看>>
NSTextView 文字链接的定制化
查看>>
第五天站立会议内容
查看>>
ATMEGA16 IOport相关汇总
查看>>
JAVA基础-多线程
查看>>
面试题5:字符串替换空格
查看>>
[Codevs] 线段树练习5
查看>>
Amazon
查看>>
component-based scene model
查看>>
Echart输出图形
查看>>