Aug 26, 2022 iOS

How to adjust SwiftUI text alignment

SwiftUI text alignment provides a lot of useful controls for aligning views, and I’d want to walk you through each one so you can understand how they work.

We can se the Frame to adjust the width and Height of the Text which we want to change the frame parameter of the Text . We can also allign the Frame width and Height of it

Text("Hello SwiftUI")
    .frame(width: 300, height: 300)

We can also use the geometryReader for accessing the device size and frame height and width also we can use the proportionality by using it.

Text("Hello SwiftUI")
    .frame(width: geometry.size.widht, height: geometry.size.height)

To change the SwiftUI Text alignment we can use the alignment() parameter inside the frame .The alignment parameter adjust the text alignment based on the use cases of the user.

Text("Hello SwiftUI")
    .frame(width: geometry.size.widht , height: geometry.size.height , alignment: .topLeading)

Alignment of SwiftUI are varied based on the following categories

  • TopLeading
  • Leading
  • bottomleading
  • Top
  • Center
  • Bottom
  • TopTrailing
  • Trailing
  • BottomTrailing

How to align text horizontally in swiftui

In SwiftUI, you can use the HStack and Spacer views to align text horizontally. Here are a few examples:

  1. Center-aligned text:

HStack {
    Spacer()
    Text("Hello, world!")
    Spacer()
}
 
  1. Left-aligned text:
HStack {
    Text("Hello, world!")
    Spacer()
}
  1. Right-aligned text:
HStack {
    Spacer()
    Text("Hello, world!")
}

In each of these examples, the Spacer view takes up the remaining space in the HStack, pushing the text to the left, center, or right, depending on where the Spacer is located. You can adjust the amount of space taken up by the Spacer by specifying a minLength parameter, like this:

Spacer(minLength: 20)

This will create a Spacer with a minimum width of 20 points.

How to adjust SwiftUI text alignment using multilineTextAlignment()

To adjust the text alignment of a SwiftUI Text view, you can use the .multilineTextAlignment() modifier. Here’s an example:

Text("Hello, world!")
    .font(.title)
    .multilineTextAlignment(.center)

In this example, we’ve created a Text view with the string “Hello, world!” and set the font to .title. We then use the .multilineTextAlignment() modifier to set the text alignment to .center.

There are several alignment options available, including .leading, .center, and .trailing. You can also use .justified to justify the text, or .natural to use the default alignment for the text’s script.

Here’s an example that demonstrates all of these alignment options:

Text("Hello, world!")
    .font(.title)
    .multilineTextAlignment(.leading)
Text("Hello, world!")
    .font(.title)
    .multilineTextAlignment(.center)
Text("Hello, world!")
    .font(.title)
    .multilineTextAlignment(.trailing)
Text("Hello, world!")
    .font(.title)
    .multilineTextAlignment(.justified)
Text("مرحبا بالعالم")
    .font(.title)
    .multilineTextAlignment(.natural)

In this example, we’ve created five Text views with different alignment options. The first three use .leading, .center, and .trailing, respectively. The fourth uses .justified to justify the text, and the fifth uses .natural to use the default alignment for the text’s script.

How to align text multi-directional in swiftui

To align text multi-directional in SwiftUI, you can use the .multilineTextAlignment() modifier along with the .lineLimit() and .minimumScaleFactor() modifiers. Here’s an example:

Text("مرحبا بالعالم\nHello, World!")
    .font(.title)
    .lineLimit(nil)
    .minimumScaleFactor(0.5)
    .multilineTextAlignment(.center)

In this example, the text contains two lines in different languages. The .multilineTextAlignment(.center) modifier centers the text horizontally within its frame, while the .lineLimit(nil) modifier removes any line limit and allows the text to wrap based on its content. The .minimumScaleFactor(0.5) modifier allows the text to shrink in size if necessary to fit within its frame.

You can adjust the .multilineTextAlignment(), .lineLimit(), and .minimumScaleFactor() modifiers to suit your needs, depending on the length and content of your text and the desired alignment. For example, you can use .multilineTextAlignment(.leading) or .multilineTextAlignment(.trailing) to align the text to the left or right, respectively, instead of centering it horizontally.

Index