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 自定義字體

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/392028.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/392028.shtml
英文地址,請注明出處:http://en.pswp.cn/news/392028.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

truncate 、delete與drop區別

相同點: 1.truncate和不帶where子句的delete、以及drop都會刪除表內的數據。 2.drop、truncate都是DDL語句(數據定義語言),執行后會自動提交。 不同點: 1. truncate 和 delete 只刪除數據不刪除表的結構(定義)drop 語句將刪除表的結構被依賴的約束(const…

Java—jsp編程

1. 編寫 login.jsp&#xff0c;登錄時只輸入一個昵稱。但要檢查昵稱是否已經被其他用戶使用。 源代碼 Login.jsp <% page contentType"text/html;charsetUTF-8" language"java" %><%request.setCharacterEncoding("UTF-8"); //設置編…

Java 8 Optional類深度解析

2019獨角獸企業重金招聘Python工程師標準>>> 身為一名Java程序員&#xff0c;大家可能都有這樣的經歷&#xff1a;調用一個方法得到了返回值卻不能直接將返回值作為參數去調用別的方法。我們首先要判斷這個返回值是否為null&#xff0c;只有在非空的前提下才能將其作…

鴿子 迷信_人工智能如何幫助我戰勝鴿子

鴿子 迷信鴿子回避系統 (Pigeon Avoidance System) Disclaimer: You are reading Part 1 that gives an overview of the project. Part 2 describes the technical setup and data collection. Part 3 is about how to train the Pigeon Recognition Model and run it on Rasp…

華為鴻蒙會議安排,2020華為HDC日程確定,鴻蒙、HMS以及EMUI 11成最關注點

原標題&#xff1a;2020華為HDC日程確定&#xff0c;鴻蒙、HMS以及EMUI 11成最關注點HDC&#xff1a;華為開發者大會&#xff0c;目前已經確定將在9月10日正式開幕。日前華為已經在其官網公布了HDC的日程&#xff0c;從現在的消息看華為開發者大會有三大點最受業內關注。鴻蒙操…

反射、元類

一、反射 1、什么是反射&#xff1a;就是反省&#xff0c;自省的意思 反射指的是一個對象應該具備&#xff0c;可以增、刪、改、查屬性的能力&#xff0c;通過字符串來操作屬性 涉及的四個函數&#xff0c;這四個函數就是普通的內置函數&#xff0c;只是沒有下劃線而已&#xf…

Java—簡單的圖書管理系統

簡單的圖書管理系統 通過數據源和DAO對象訪問數據庫。其中JavaBeans實現模型&#xff0c;訪問數據庫&#xff0c;Servlet實現控制器&#xff0c;JSP頁面實現視圖。 ? 模型包括2個JavaBean&#xff1a;BookBean用于存放圖書信息&#xff0c;BookDAO用于訪問數據庫。 ? 控制器包…

成功的秘訣是什么_學習編碼的10個成功秘訣

成功的秘訣是什么This post was originally published on Coder-Coder.com.該帖子最初發布在Coder-Coder.com上 。 If you’re teaching yourself how to code, you may have more questions than answers when you’re starting out.如果您正在教自己如何編碼&#xff0c;那么…

ZJUT 地下迷宮 (高斯求期望)

http://cpp.zjut.edu.cn/ShowProblem.aspx?ShowID1423 設dp[i]表示在i點時到達終點要走的期望步數&#xff0c;那么dp[i] ∑1/m*dp[j] 1&#xff0c;j是與i相連的點&#xff0c;m是與i相鄰的點數。建立方程組求解。重要的一點是先推斷DK到達不了的點。須要bfs預處理一下進行…

html收款頁面模板,訂單收款.html

&#xfeff;訂單收款$axure.utils.getTransparentGifPath function() { return resources/images/transparent.gif; };$axure.utils.getOtherPath function() { return resources/Other.html; };$axure.utils.getReloadPath function() { return resources/reload.html; };…

pandas之時間數據

1.時間戳Timestamp() 參數可以為各種形式的時間&#xff0c;Timestamp()會將其轉換為時間。 time1 pd.Timestamp(2019/7/13) time2 pd.Timestamp(13/7/2019 13:05) time3 - pd.Timestamp(2019-7-13) time4 pd.Timestamp(2019 7 13 13:05) time5 pd.Timestamp(2019 July 13 …

scikit keras_Scikit學習,TensorFlow,PyTorch,Keras…但是天秤座呢?

scikit kerasWelcome all! In the first episode of this series, I investigated the four most known machine learning frameworks and discussed which of these you should learn depending on your needs and goals.w ^迎閱讀所有&#xff01; 在本系列的第一集中 &#…

程序員如何學習更好的知識_如何保持學習并成為更好的程序員

程序員如何學習更好的知識by Kevin Gardner凱文加德納(Kevin Gardner) 如何保持學習并成為更好的程序員 (How to keep learning and become a better coder) Coding has come a long way since the days of Robert Taylor and ARPANET and Sir Tim Berners-Lee and CERN — an…

Educational Codeforces Round 25 C. Multi-judge Solving

題目鏈接&#xff1a;http://codeforces.com/contest/825/problem/C C. Multi-judge Solving time limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputMakes solves problems on Decoforces and lots of other different onli…

Java—stream以及集合框架使用

1) 編寫Student類&#xff0c;主要屬性包括學號、姓名、性別、班級 2) 編寫Score類&#xff0c;主要屬性包括&#xff1a;學號、課程名、分數 3) 模擬期末考試的成績統計應用場景&#xff0c;要求 (1) 所有學生名單及對應科目成績已經初始化在數組中 (2) 要求輸出每門課程的所有…

山東省2021年高考成績查詢平臺6,山東2021年高考成績改為6月26日前公布

6月11日&#xff0c;山東省教育廳舉行2021年第一次高考新聞發布會&#xff0c;介紹2021年高考基本情況、評卷安排、成績公布等相關工作。山東省教育招生考試院新聞發言人、普招處處長李春光介紹&#xff0c;根據近期國家有關工作要求和強基計劃招生工作需要&#xff0c;原定于6…

如何在vuejs里禁用eslint語法檢查工具

eslint好是好&#xff0c;可要求很苛刻&#xff0c;對于我這種寫代碼很糙的媛。。。。。。 搜索的時候有的說加入 /* eslint-disabled */&#xff08;有用&#xff0c;但只是部分代碼享受此待遇&#xff09; 還有說刪除.eslintrc.js里包含eslint關鍵字的塊&#xff0c;a---o---…

數據結構兩個月學完_這是我作為數據科學家兩年來所學到的

數據結構兩個月學完It has been 2 years ever since I started my data science journey. Boy, that was one heck of a roller coaster ride!自從我開始數據科學之旅以來已經有兩年了 。 男孩 &#xff0c;那可真是坐過山車&#xff01; There were many highs and lows, and…

leetcode 888. 公平的糖果棒交換(set)

愛麗絲和鮑勃有不同大小的糖果棒&#xff1a;A[i] 是愛麗絲擁有的第 i 根糖果棒的大小&#xff0c;B[j] 是鮑勃擁有的第 j 根糖果棒的大小。 因為他們是朋友&#xff0c;所以他們想交換一根糖果棒&#xff0c;這樣交換后&#xff0c;他們都有相同的糖果總量。&#xff08;一個…

如何使用JavaScript檢查輸入是否為空

by Zell Liew由Zell Liew 如何使用JavaScript檢查輸入是否為空 (How to check if an input is empty with JavaScript) Last week, I shared how to check if an input is empty with CSS. Today, let’s talk about the same thing, but with JavaScript.上周&#xff0c;我分…