45

Creating a List as follows:

struct ContentView: View {
    var body: some View {
        List {
            Section(header: Text("Header")) {
                Text("Row 1")
                Text("Row 2")
            }
        }
        .listStyle(PlainListStyle())
    }
}

uses uppercase text in the section header.

enter image description here

Is there a way to force the header text to be retain its original case?

Andy Jazz
  • 49,178
  • 17
  • 136
  • 220
Ashley Mills
  • 50,474
  • 16
  • 129
  • 160

4 Answers4

97

There's a textCase(nil) modifier on Section that honours the original text case, which works on iOS 14

From Apple's developer forums: https://developer.apple.com/forums/thread/655524

Section(header: Text("Section Title")) {
    [...]
}.textCase(nil)
axes78
  • 986
  • 6
  • 2
21

For a solution that works with both iOS 13 and 14, you can make a custom modifier that only sets the textCase for iOS 14:

struct SectionHeaderStyle: ViewModifier {
    func body(content: Content) -> some View {
        Group {
            if #available(iOS 14, *) {
                AnyView(content.textCase(.none))
            } else {
                content
            }
        }
    }
}

And then you can apply it to your section like this:

Section(header: Text("Section Name")) {
 ...
}.modifier(SectionHeaderStyle())

This is an adapted version of a suggestion from apple forums: https://developer.apple.com/forums/thread/650243

emma
  • 419
  • 5
  • 8
5

I would like to add my solution which I find very convenient dealing with this issue. I simply made a custom text component that I use for section headers.

import SwiftUI

struct SectionHeaderText: View {
    var text: String

    var body: some View {
        if #available(iOS 14.0, *) {
            Text(text).textCase(nil)
        } else {
            Text(text)
        }
    }
}

I then use it like this:

Section(header: SectionHeaderText(text: "Info"), ...

Mitigates the whole situation with #available(iOS 14.0, *) and gets me the result I want :) Maybe this helps someone out there!

TurboFish
  • 7,789
  • 4
  • 18
  • 27
0

List's Style in iOS 16.4

In iOS 16.4, .plane and .inset type properties do provide the expected behavior:

struct ContentView : View {
    var body: some View {
        List {
            Section(header: Text("Header")) {
                Text("Row 1")
                Text("Row 2")
            }
        }
        .listStyle(.plain)
        // .listStyle(.inset)
    }
}

.plane and .inset properties help you retain the original letters' case:

List Style Text Appearance Extra Features
.automatic HEADER
.grouped HEADER
.inset Header
.insetGrouped HEADER
.plain Header section header's BG color is .clear
.sidebar HEADER section's disclosure indicator
Andy Jazz
  • 49,178
  • 17
  • 136
  • 220