日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

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

發布時間:2023/11/29 编程问答 37 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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 自定義字體

總結

以上是生活随笔為你收集整理的ios 自定义字体_如何仅用几行代码在iOS应用中创建一致的自定义字体的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。