Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SwiftUI/UIKit How to convert SwiftUI Font to UIKit UIFont?

Tags:

swiftui

Due to some limitations in SwiftUI Text. I've to use the UIText instead.

In an UIViewRepresentable,

    func makeUIView(context: Context) -> UITextView {
        let vw = UITextView()
        let env = context.environment
        
        // UIFont?     Font?
        vw.font = env.font

        ...
    }

I want to initialize the UIKit UILabel using the SwiftUI EnvironmentValues.

More specifically, assign an SwiftUI Font to UIKit UIFont.

The question is, how to convert a Font to UIFont?

like image 460
zrfrank Avatar asked Oct 15 '20 09:10

zrfrank


People also ask

What is uifont in SwiftUI?

The UIFont class is a programmatic interface to access font characteristics and provide the system with glyph information of the font. In SwiftUI UIFont has been simplified to a system class called Font that makes it easier to set custom fonts to be used by the application!

How to use fonts in SwiftUI and customize text view?

Learn how to use fonts in SwiftUI and customize Text view in SwiftUI with number of Font options such as design, size, weight, color and others. SwiftUI lets you customize Text by applying a .font () modifier. The default iOS font is called San Francisco and if you don’t explicitly change it, then all of your text will have the default iOS look.

How do I change the font in a UIKit project?

In order to use the new font in a UIKit project, set the label’s font to custom UIFont like that: You can find out what other fonts are already available on iOS to use in your app by running the following code which prints out all font names:

Is'uifont'convertible to'ctfont'?

Error: 'UIFont' is not convertible to 'CTFont' uikitswiftcore-text Share Improve this question Follow edited Jul 9 '14 at 8:53 Christopher Lenz asked Jun 27 '14 at 16:40 Christopher LenzChristopher Lenz 43055 silver badges1212 bronze badges 1


1 Answers

If you need share same font, then use UIFont as model property and convert in SwiftUI where needed as, for example

Text("Some text")
   .font(Font(uiFont as CTFont))
like image 175
Asperi Avatar answered Oct 19 '22 02:10

Asperi