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

只愿长相守 提交于 2021-02-11 11:01:53

问题


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?


回答1:


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


来源:https://stackoverflow.com/questions/64369016/swiftui-uikit-how-to-convert-swiftui-font-to-uikit-uifont

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!