55

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?

4
  • unfortunately, not yet (at least, I didn't find any way), fill the radar ... Commented Feb 4, 2020 at 10:27
  • Keep in mind: SwiftUI.Font is a View, where UITextView.font is a UIFont Commented Feb 7, 2020 at 17:39
  • 10
    @ninestones SwiftUI.Font is just a plain Struct conforms to Equatable and Hashable, not a View: developer.apple.com/documentation/swiftui/font
    – Jonny
    Commented Jun 27, 2020 at 6:23
  • 1
    github.com/LeoNatan/LNSwiftUIUtils I've created a small library, which includes a Font -> UIFont conversion method. It supports all modifiers but the leading() one.
    – Léo Natan
    Commented Oct 22, 2023 at 17:49

4 Answers 4

19

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
    }
}
3
  • 12
    This does not support custom font designs (rounded, etc), custom sizes/weights or custom fonts. I suggest people create feedbacks for Apple to support retrieving a UIFont or NSFont instance from Font directly. Commented Dec 26, 2020 at 8:32
  • 2
    @Luke_Howard perhaps change the method name to preferredFont(from font:Font) to reflect that this only returns default system font. Commented Dec 26, 2020 at 8:45
  • I don't think that this answers the OP's question. There's the assumption of using the default font. What if the OP loaded a custom font?
    – RobMac
    Commented Nov 19, 2022 at 17:47
13

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)
   }
}

From Swift 5.9, you can use the switch statement to return a value and have an even less cluttered look:

extension UIFont {
  class func preferredFont(from font: Font) -> UIFont {
      let style: UIFont.TextStyle =
      switch font {
        case .largeTitle:   .largeTitle
        case .title:        .title1
        case .title2:       .title2
        case .title3:       .title3
        case .headline:     .headline
        case .subheadline:  .subheadline
        case .callout:      .callout
        case .caption:      .caption1
        case .caption2:     .caption2
        case .footnote:     .footnote
        default: /*.body */ .body 
      }
      return  UIFont.preferredFont(forTextStyle: style)
    }
 }
3
  • 1
    Nice piece of re-factoring.
    – Grimxn
    Commented May 30, 2023 at 9:49
  • Note that this works equally well for NSFont.
    – Grimxn
    Commented May 30, 2023 at 11:46
  • I have added a version that works with the new syntax feature in Swift 5.9, of allowing switch to return a value Commented Oct 4, 2023 at 4:59
4

It's a good question and although as mentioned in the other answers, you can't convert SwiftUI.Font directly to UIFont, but you can easily work around it by using Foundation's AttributedString. Here's how I did it for UILabel:

func updateUIView(_ label: UILabel, context: Context) {
    var container = AttributeContainer()
    container.font = context.environment.font

    let attributedString = AttributedString(text, attributes: container)
    label.attributedText = NSAttributedString(attributedString)
}
2
  • (A) I'm not following how this addresses the question of finding a UIFont that corresponds to a given SwiftUI.Font. (B) It sounds like you provided a workaround, but I'm not seeing the connection yet. Could you flesh out your explanation please?
    – David J.
    Commented Jan 28 at 19:20
  • Hey @DavidJ. you're right. I tried to give an answer to the question, which was about using it in a UIViewRepresentable with UITextView. You could perhaps (ab)use the AttributeContainer to convert between the two by using the swiftUI and uiKit atributes on that container: developer.apple.com/documentation/foundation/attributescopes
    – lennartk
    Commented Feb 9 at 2:21
2

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.