2

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,070
  • 4
  • 44
  • 61

2 Answers2

4

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
  • 121
  • 2
  • 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
  • 10,037
  • 2
  • 21
  • 33