Convert from SwiftUI.Font to UIFont

二次信任 提交于 2020-08-01 03:57:10

问题


I have a UIViewRepresentable with a UITextView and I want to set the font on the UITextView using the Font from the SwiftUI environment. I just need a simple init like:

UIFont(_ swiftUIFont: Font)

But I can't find any such thing. And the Font type doesn't seem to have any information I can use to try to implement one. Anyone know of a way to convert between the two?


回答1:


Font seems to be based on top of Core Text, rather than on UIKit.

You could create a shared CTFont, and share it between UIKit and SwiftUI.

 // Shared font
 let ctFont = CTFontCreateUIFontForLanguage(.system, 12, nil)!
 // UIKit
 let uiFont = UIFont(name: CTFontCopyPostScriptName(ctFont) as String, size: CTFontGetSize(ctFont))
 // SwiftUI
 let swiftUiFont = Font(ctFont)


来源:https://stackoverflow.com/questions/60049927/convert-from-swiftui-font-to-uifont

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