2

How to use UIFont in SwiftUI Font

 2 years ago
source link: https://sarunw.com/posts/uifont-to-font/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

How to use UIFont in SwiftUI Font


Table of Contents

How to convert UIFont to Font

We can convert UIKit UIFont to SwiftUI Font using init(_ font: CTFont) initializer.

The initializer accepts CTFont as an argument. The CTFont is an opaque type that represents a font object.

Both NSFont and UIFont can toll-free bridged[1] to CTFont.

Here is an example of converting UIKit UIFont to SwiftUI Font.

let uiFont = UIFont.systemFont(ofSize: 18, weight: .bold)
// 1
let font = Font(uiFont)

1 As you can see, we can initialize Font by passing an UIFont instance as an argument.

How to convert Font to UIFont

We can easily convert UIFont to Font, but not another way round. But this might not be a problem since UIFont contains much more flexibility and method than Font.

Anything that you can do with Font, you probably find an equivalent way to do it in UIFont.



You may also like

SF Font Expanded, Condensed, and Compressed: Three New font width styles in iOS 16

In iOS 16, Apple introduces three new width styles to the SF font family. Let's see what they look like and how to use them.

SwiftUI UIKit Font Text
How to change SwiftUI font size

There are two ways to set a font size for Text view in SwiftUI. Let's see what they are and how to set them.

SwiftUI Font Text
How to style SwiftUI text Font

Learn how to set font size, design, weight, and color in SwiftUI.

SwiftUI Font Text

Read more article about SwiftUI, UIKit, Font, Text,

or see all available topic

Enjoy the read?

If you enjoy this article, you can subscribe to the weekly newsletter.
Every Friday, you'll get a quick recap of all articles and tips posted on this site. No strings attached. Unsubscribe anytime.

Feel free to follow me on Twitter and ask your questions related to this post. Thanks for reading and see you next time.

If you enjoy my writing, please check out my Patreon https://www.patreon.com/sarunw and become my supporter. Sharing the article is also greatly appreciated.

Become a patron

Buy me a coffee

Tweet

Share

Previous
How to make SwiftUI button with buttonStyle expand to full width

SwiftUI got many beautiful built-in button styles. One problem you might get is it isn't obvious how to control the size of it. Let's learn how to do it.

Next
SF Font Expanded, Condensed, and Compressed: Three New font width styles in iOS 16

In iOS 16, Apple introduces three new width styles to the SF font family. Let's see what they look like and how to use them.

← Home


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK