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.
You can easily support sarunw.com by checking out this sponsor.
AI Grammar: Correct grammar, spell check, check punctuation, and parphrase.
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
.
Toll-free bridging means you can use that bridged data type interchangeably as a parameter without any casting https://developer.apple.com/library/archive/documentation/General/Conceptual/CocoaEncyclopedia/Toll-FreeBridgin/Toll-FreeBridgin.html. ↩︎
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 ShareHow 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.
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.