Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SwiftUI Section header - use non uppercase?

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?

like image 927
Ashley Mills Avatar asked Jul 06 '20 09:07

Ashley Mills


3 Answers

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)
like image 82
axes78 Avatar answered Nov 05 '22 12:11

axes78


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

like image 31
emma Avatar answered Nov 05 '22 10:11

emma


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!

like image 5
TurboFish Avatar answered Nov 05 '22 12:11

TurboFish