PrefaceIn iOS 16, Apple introduced three new width style fonts to the SF font library.
UIFont.WidthApple introduced a new structure UIFont.Width, which represents a new width style. There are currently four styles.
SF font and new width stylesHow to use SF fonts with the new width style To use the new width style, Apple has a new UIFont class method that accepts the new UIFont.Width . class UIFont : NSObject { You can use the new method as you normally would when creating fonts. let condensed = UIFont.systemFont(ofSize: 46, weight: .bold, width: .condensed) SwiftUI
Currently (Xcode 16 beta 6), this new width style and initializer is only available in UIKit, but luckily, we can easily use it in SwiftUI. There are many ways to integrate UIKit into SwiftUI. I’ll show you two ways to use the new width styles in SwiftUI.
Convert UIfont to FontWe know from how to convert UIFont to Font in SwiftUI [1] that Font has an initializer method that receives a UIFont as a parameter. The steps are as follows
struct NewFontExample: View {
Creating a Font extensionThis method is actually the same as converting UIfont to Font. We just need to create a new Font extension to make it easier to use in SwiftUI. extension Font { Create a static function to pass the parameters required by UIFont. Then, initialize UIFont and create Font. We can use it like this. Text("Compressed") How to use the new width styleYou can use it anywhere you want. There won't be any restrictions, all new widths will have the same dimensions, same height, only the width will change. Here is the same text, same font size and same font style with different font width styles. New Width Style AdvantagesYou can use new width styles on top of existing font styles, such as thin or bold, to create a unique experience in your app. Apple uses it in their Photos app, in the "Memories" feature, by combining different font widths and styles in the title or subtitle. Here are some font combinations of different widths and styles to inspire you. Text("Pet Friends") You can also use the new width styles to control the readability of text. The following example shows how different width styles affect the number of characters per line and paragraph length: Download this fontYou can download these new font width styles from the Apple Font Platform[2]. After downloading and installing, you will find a new style that combines the existing width styles with the new width styles.
References[1] How to convert UIFont to Font in SwiftUI: https://www.jianshu.com/p/56ee0d1ea0e1. [2] Apple Font Platform: https://developer.apple.com/fonts/. |
<<: Sourcery Swift Package command line plugin
>>: Creating a Line Chart with SwiftUI Charts in iOS 16
(Image source: CCTV.com screenshot) These two pho...
1. How much does it cost for a company to promote...
With the continuous efforts of domestic automaker...
In my memory, 10 years is not a very short period...
On February 15, the official website of New Weekl...
【Today’s cover】 On the 19th, on Kaisha Island in ...
In real life, many people have two criteria for j...
It has been nearly four years since the term &quo...
The reason why Tik Tok is so popular is definitel...
If you were to ask what examination is the most t...
Since its launch in 2008, Apple App Store has bee...
I recently saw an interesting content: "Why ...
Looking down at the earth from the vast space 705...
6 Lessons for Effective HR Managers [Complete wit...
Often, people tend to compare the pros and cons o...