swiftui

How to convert SwiftUI Font to UIKit UIFont?


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?


Solution

  • 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))