SwiftUI/UIKit 如何将 SwiftUI 字体转换为 UIKit UIFont? [英] SwiftUI/UIKit How to convert SwiftUI Font to UIKit UIFont?

查看:40
本文介绍了SwiftUI/UIKit 如何将 SwiftUI 字体转换为 UIKit UIFont?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于 SwiftUI 文本中的一些限制.我必须改用 UIText.

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

在 UIViewRepresentable 中,

In an UIViewRepresentable,

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

        ...
    }

我想使用 SwiftUI EnvironmentValues 初始化 UIKit UILabel.

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

更具体地说,为 UIKit UIFont 分配一个 SwiftUI 字体.

More specifically, assign an SwiftUI Font to UIKit UIFont.

问题是,如何将 Font 转换为 UIFont?

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

推荐答案

如果你需要共享相同的字体,那么使用 UIFont 作为模型属性并在需要的地方在 SwiftUI 中转换,例如

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

这篇关于SwiftUI/UIKit 如何将 SwiftUI 字体转换为 UIKit UIFont?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆