I have a UIViewRepresentable
with a UITextView
and I want to set the font on the UITextView
using the current 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 font representations?
Setting Text and Changing Fonts The most basic way we can add text in SwiftUI is by the use of Text() so to make it easy lets just use this as example. Text("Hello, world!") Changing Font is easy, you just need to use the dot notation called . font which accepts a Font type value.
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. Some other options of standard fonts include: title, headline, subheadline, body, callout, caption or footnote.
system(size:weight:design:) method. This method lets us specify a fixed font size that we want. Here is an example where we set the font for the second text to a fixed size of 36 points.
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! To add a custom font, we of course need to add a font file in order to do to that. Go to your favorite font site and look for the font that you want to add.
The most basic way we can add text in SwiftUI is by the use of Text () so to make it easy lets just use this as example. Text ("Hello, world!") Changing Font is easy, you just need to use the dot notation called .font which accepts a Font type value.
Text ( "Simple Swift Guide" ). font (. headline ). bold (). italic () Text ( "Simple Swift Guide" ). font (. callout ). foregroundColor (. blue) In a second, more advanced tutorial, we’re going to learn how to use custom font family in your iOS app.
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:
A bit of a hack but works (doing the other direction is left as an exercise to the reader).
extension UIFont {
class func preferredFont(from font: Font) -> UIFont {
let uiFont: UIFont
switch font {
case .largeTitle:
uiFont = UIFont.preferredFont(forTextStyle: .largeTitle)
case .title:
uiFont = UIFont.preferredFont(forTextStyle: .title1)
case .title2:
uiFont = UIFont.preferredFont(forTextStyle: .title2)
case .title3:
uiFont = UIFont.preferredFont(forTextStyle: .title3)
case .headline:
uiFont = UIFont.preferredFont(forTextStyle: .headline)
case .subheadline:
uiFont = UIFont.preferredFont(forTextStyle: .subheadline)
case .callout:
uiFont = UIFont.preferredFont(forTextStyle: .callout)
case .caption:
uiFont = UIFont.preferredFont(forTextStyle: .caption1)
case .caption2:
uiFont = UIFont.preferredFont(forTextStyle: .caption2)
case .footnote:
uiFont = UIFont.preferredFont(forTextStyle: .footnote)
case .body:
fallthrough
default:
uiFont = UIFont.preferredFont(forTextStyle: .body)
}
return uiFont
}
}
No you can't, you have to use the params you passed to the Font
to create UIFont
Here is a reformatting of Luke Howard's solution to reduce the amount of text
extension UIFont {
class func preferredFont(from font: Font) -> UIFont {
let style: UIFont.TextStyle
switch font {
case .largeTitle: style = .largeTitle
case .title: style = .title1
case .title2: style = .title2
case .title3: style = .title3
case .headline: style = .headline
case .subheadline: style = .subheadline
case .callout: style = .callout
case .caption: style = .caption1
case .caption2: style = .caption2
case .footnote: style = .footnote
case .body: fallthrough
default: style = .body
}
return UIFont.preferredFont(forTextStyle: style)
}
}
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