【问题标题】:SwiftUI Section header - use non uppercase?SwiftUI 部分标题 - 使用非大写?
【发布时间】:2020-10-26 09:57:59
【问题描述】:

如下创建列表:

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

在部分标题中使用大写文本。

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

【问题讨论】:

  • 您的代码在 Xcode 11.5、iOS 13 中按预期工作。节标题是“Header”(大写但不是大写)。
  • 用 Xcode 11.5 在模拟器中测试它显示“Header”
  • 在 beta 2 中仍然是一个问题
  • 在 beta 3 中仍然是一个问题
  • 在 beta 4 中仍然是一个问题

标签: swiftui swiftui-list ios14


【解决方案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, *) 缓解整个情况并得到我想要的结果:) 也许这可以帮助那里的人!

【讨论】:

    【解决方案2】:

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

    【讨论】:

    • 这不应该应用于 Text("Section Name") 而不是整个部分吗?
    【解决方案3】:

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

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

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

    【讨论】:

    • 我在下面发布了一个试图缓解#available(iOS 14.0, *) 问题的答案!否则,这正是我的方式。
    • 你也可以直接在标题上调用textCase而不是在Section上
    猜你喜欢
    • 2021-10-22
    • 2020-01-27
    • 1970-01-01
    • 2010-11-17
    • 2021-11-27
    • 1970-01-01
    • 1970-01-01
    • 2021-11-11
    • 1970-01-01
    相关资源
    最近更新 更多