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
相关产品推荐
相关产品推荐

