SwiftUI Section标头-使用非大写字母吗?

时间:2020-07-06 09:41:03

标签: swiftui swiftui-list ios14

创建列表如下:

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

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

enter image description here

是否有一种方法可以强制标题文本保留其原始大小写?

3 个答案:

答案 0 :(得分:23)

Section上有一个textCase(nil)修饰符,用于纪念原始文本大小写,该修饰符可在iOS 14上使用

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

Section(header: Text("Section Title")) {
    [...]
}.textCase(nil)

答案 1 :(得分:9)

对于适用于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

答案 2 :(得分:1)

我想添加我的解决方案,我发现该解决方案非常方便。我只是制作了一个用于节标题的自定义文本组件。

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, *)减轻整体情况,并获得我想要的结果:)也许这可以帮助某个人!