You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

SwiftUI未设置相关属性,Alert文本却自动显示为大写?

SwiftUI Alert标题自动大写的解决方法

这是iOS系统对Alert标题默认应用了.uppercase文本大小写规则导致的,属于系统默认UI样式,并非你的代码设置问题。以下是两种可行的解决方式:

方法1:使用AttributedString自定义标题样式

直接给Alert标题的文本取消自动大小写设置:

.alert(isPresented: $showingAlert) {
    Alert(
        title: Text(AttributedString("Do you want to delete the event and its details?")
            .setting(\.textCase, nil)),
        primaryButton: .destructive(Text("Delete")),
        secondaryButton: .cancel()
    )
}

方法2:通过环境变量取消自动大写

可以在Alert所在的视图层级添加环境变量,全局取消当前视图范围内的文本自动大小写规则:

// 在视图的末尾添加环境设置
YourView()
    .environment(\.textCase, nil)

或者仅针对Alert的标题文本单独设置:

.alert(isPresented: $showingAlert) {
    Alert(
        title: Text("Do you want to delete the event and its details?")
            .environment(\.textCase, nil),
        primaryButton: .destructive(Text("Delete")),
        secondaryButton: .cancel()
    )
}

内容的提问来源于stack exchange,提问作者Dipesh Pokhrel

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.21 18:42:38