4

Is there a way to get/obtain the current font used in a Text object in SwiftUI? ex. Text("abc").font() does not work.

andrewz
  • 4,729
  • 5
  • 49
  • 67

2 Answers2

6

The current font is accessible from the environment:

struct ChildView: View {
    @Environment(\.font) var font

    var body: some View {
        Text("Italic version of the hierarchy's font")
            .font((font ?? .body).italic())
    }
}

See https://developer.apple.com/documentation/swiftui/environmentvalues for the full list of available keys, they can come in handy.

kyis
  • 181
  • 4
  • 4
-1

You can use systemFont.

 Text("ddd").font(Font.system(size: 50))

So you don't need to know exact name of the font.

E.Coms
  • 11,065
  • 2
  • 23
  • 35