Contents

baselineOffset(_:)

Sets the vertical offset for the text relative to its baseline.

Declaration

nonisolated func baselineOffset(_ baselineOffset: CGFloat) -> Text

Parameters

  • baselineOffset:

    The amount to shift the text vertically (up or down) relative to its baseline.

Return Value

Text that’s above or below its baseline.

Discussion

Change the baseline offset to move the text in the view (in points) up or down relative to its baseline. The bounds of the view expand to contain the moved text.

HStack(alignment: .top) {
    Text("Hello")
        .baselineOffset(-10)
        .border(Color.red)
    Text("Hello")
        .border(Color.green)
    Text("Hello")
        .baselineOffset(10)
        .border(Color.blue)
}
.background(Color(white: 0.9))

By drawing a border around each text view, you can see how the text moves, and how that affects the view.

[Image]

The first view, with a negative offset, grows downward to handle the lowered text. The last view, with a positive offset, grows upward. The enclosing HStack instance, shown in gray, ensures all the text views remain aligned at their top edge, regardless of the offset.

See Also

Styling the view’s text