skip to Main Content

Is there a way to prevent wrapped text in a DisclouseGroup title from being center aligned?

Disclosure group with center aligned text

I have tried to add the following, but neither approach has been effective:

DisclosureGroup("A really long disclosure group title that is being center aligned.", isExpanded: false) {
   ...
}
.multilineTextAlignment(.leading)
DisclosureGroup("A really long disclosure group title that is being center aligned.", isExpanded: false) {
   ...
}
.frame(maxWidth: .infinity, alignment: .leading)

2

Answers


  1. 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

    Login or Signup to reply.
  2. You can wrap it in a Form or a List:

    struct DisclosureAligning: View {
    
        @State var isExpanded = false
    
        var body: some View {
            Form {
                DisclosureGroup("A really long disclosure group title that is being center aligned.", isExpanded: $isExpanded) {
                    Text("text")
                }
            }
        }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search