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?
The UIFont class is a programmatic interface to access font characteristics and provide the system with glyph information of the font. In SwiftUI UIFont has been simplified to a system class called Font that makes it easier to set custom fonts to be used by the application!
Learn how to use fonts in SwiftUI and customize Text view in SwiftUI with number of Font options such as design, size, weight, color and others. SwiftUI lets you customize Text by applying a .font () modifier. The default iOS font is called San Francisco and if you don’t explicitly change it, then all of your text will have the default iOS look.
In order to use the new font in a UIKit project, set the label’s font to custom UIFont like that: You can find out what other fonts are already available on iOS to use in your app by running the following code which prints out all font names:
Error: 'UIFont' is not convertible to 'CTFont' uikitswiftcore-text Share Improve this question Follow edited Jul 9 '14 at 8:53 Christopher Lenz asked Jun 27 '14 at 16:40 Christopher LenzChristopher Lenz 43055 silver badges1212 bronze badges 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))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With