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
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))
}
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
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:
- set
maxHeight
to.infinity
, so it can expand vertically too - 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:
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
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
Xcode Error While Validation - "Your Binary Is Not Optimized for iPhone 5"
How to Completely Hide the Status Bar in iOS Using Cordova
Open Links in Safari Instead of Uiwebview
Add Cocoapods to Tests Target Too
Memory Leak When Using Nsurlsession.Downloadtaskwithurl
Ios: Is Core Graphics Implemented on Top of Opengl
How to Position Views on Top of Each Other
Abpeoplepickernavigationcontroller Changes with iOS8
When Does Awakefromnib Get Called
Accessing Variables from Another Viewcontroller in Swift
How to Change the Blue Highlight Color of a Uitableviewcell
Limit Number of Characters in Uitextview