Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert from SwiftUI.Font to UIFont

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?

like image 481
Richard Venable Avatar asked Feb 04 '20 02:02

Richard Venable


People also ask

How do I change the font in SwiftUI?

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.

What is the default font for SwiftUI?

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.

How do I change font size in SwiftUI?

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.

How to add a custom font in SwiftUI uifont?

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.

How do I add text in SwiftUI?

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.

What are the different types of fonts used in Swift?

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.

How do I change the font in a UIKit project?

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:


3 Answers

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
    }
}
like image 195
Luke Howard Avatar answered Oct 12 '22 05:10

Luke Howard


No you can't, you have to use the params you passed to the Font to create UIFont

like image 45
JIE WANG Avatar answered Oct 12 '22 05:10

JIE WANG


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)
   }
}
like image 5
MarkAurelius Avatar answered Oct 12 '22 05:10

MarkAurelius