32

创建列表如下:

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

在节标题中使用大写文本。

在此处输入图像描述

有没有办法强制标题文本保留其原始大小写?

4

3 回答 3

62

Section 上有一个 textCase(nil) 修饰符,它尊重原始文本大小写,适用于 iOS 14

来自 Apple 的开发者论坛:https ://developer.apple.com/forums/thread/655524

Section(header: Text("Section Title")) {
    [...]
}.textCase(nil)
于 2020-08-01T05:18:14.197 回答
19

对于同时适用于 iOS 13 和 14 的解决方案,您可以制作一个仅设置 iOS 14 的 textCase 的自定义修饰符:

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

然后你可以像这样将它应用到你的部分:

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

这是苹果论坛建议的改编版本:https ://developer.apple.com/forums/thread/650243

于 2020-09-23T20:06:07.223 回答
4

我想添加我的解决方案,我觉得处理这个问题非常方便。我只是制作了一个用于节标题的自定义文本组件。

import SwiftUI

struct SectionHeaderText: View {
    var text: String

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

然后我像这样使用它:

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

减轻整个情况#available(iOS 14.0, *)并得到我想要的结果:)也许这可以帮助那里的人!

于 2020-10-16T11:48:19.330 回答