问题
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