Mango Snippets » #9

SwiftUI: How to Show Subscript and Superscript Texts

SwiftUI gives us a .baselineOffset(_:) modifier to set the vertical offset for the text relative to its baseline. You can combine this with smaller fonts to show subscript and superscript texts. For example:

var body: some View {
    + Text("sub").font(.caption).baselineOffset(-3)
    + Text(" Umbrella")
    + Text("sup").font(.caption).baselineOffset(6)