Swiftui Text-Alignment

SwiftUI text-alignment

You can do this via the modifier .multilineTextAlignment(.center).

Text("CENTER")
.multilineTextAlignment(.center)

Apple Documentation

How to left align text in swift UI?

A frame alignment is for entire view (which is square), you need multi-line text alignment, like

Text(titleSring)
.multilineTextAlignment(.leading)

SwiftUI Text alignment with maxWidth not working

Because alignmentGuide has effect in container with other subviews. In this case you need to align Text within own frame.

Here is solution

demo

Text("Test")
.foregroundColor(.white)
.font(.subheadline)
.frame(maxWidth: .infinity, alignment: .leading) // << here !!
.background(Color(.blue))

SwiftUI How to align the bottom of two text with different font size?

Instead of .bottom, you can use .lastTextBaseline as your HStack alignment:

HStack(alignment: .lastTextBaseline) {
Text("Hello").font(.system(size: 40))
Text("World").font(.system(size: 10))
}

enter image description here

SwiftUI Disclosure Group Text Alignment

You can use the Label initialiser for DisclosureGroup. You can read more about it here

Here is a working example.

struct ContentView: View {

@State private var isExpanded = false
var body: some View {
VStack {
DisclosureGroup(isExpanded: $isExpanded) {
Text("This is some text")
} label: {
Text("A really long disclosure group title that is being center aligned.")
.multilineTextAlignment(.leading)
}

}
}
}

This gives the following output

enter image description here

Tested Xcode 14 beta 4 iOS 16 Simulator

How to make SwiftUI Text multilineTextAlignment start from Top and Center

For the Text's frame, you need to:

  1. set maxHeight to .infinity, so it can expand vertically too
  2. set alignment to .top, so it's aligned to the top
let names = ["Hi", "Hello world", "Long long text"]

let gridItemLayout = [
GridItem(.adaptive(minimum: 80))
]

var body: some View {

LazyVGrid(columns: gridItemLayout, spacing: 0) {
ForEach((0...7), id: \.self) { i in
VStack {
Image(systemName: "folder")
.frame(width: 100, height: 100)
.background(Color.green)
Text(names.randomElement() ?? "")
.font(.body)
.multilineTextAlignment(.center)

/// here!
.frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .top)
}
}
}
.padding(.horizontal, 25.0)
}

Result:

Grid of elements composed of an Image on top of a Text. The Image's y position is always the same, while the Text expands down when it has multiple lines.

Center alignment text inside of Text in SwiftUI

You should use the .multilineTextAlignment(_:) method on the Text element.

This works fine for me:

Text("[...]")
.lineLimit(nil)
.multilineTextAlignment(.center)
.padding(.horizontal, 40)

How to align text views differently under a vstack in swiftui?

I can assume that you just need padding in right place to have something like "margins", so a solution can be like

demo

var body: some View {
VStack(alignment: .leading) {
Text("This is the first paragraph")
.foregroundColor(Color.red)
.frame(maxWidth: .infinity) // << default center !!

Spacer().frame(height: 10)
Text("This is a paragraph with ").foregroundColor(Color.blue) + Text("multiple colors.").foregroundColor(Color.green)
Spacer().frame(height: 10)
if #available(iOS 15.0, *) {
AsyncImage(url: URL(string: "https://www.example.org/img/image.png")) { image in
image.resizable()
} placeholder: {
Color.red
}
.frame(width: 128, height: 128)
.clipShape(RoundedRectangle(cornerRadius: 25))
.frame(maxWidth: .infinity) // << for center !!
}
Spacer().frame(height: 10)
Text("Final paragraph").foregroundColor(Color.red)
Spacer()
}
.padding(.horizontal, 24) // << for margins !!
}


Related Topics



Leave a reply



Submit