Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SwiftUI: Uppercase a localized string for a view, e.g. `Text`?

Using Xcode beta 6 and SwiftUI, creating a Text view using a >localized< string, how can I uppercase it without having to make the localized string value uppercased? I guess I want a ViewModifier for changing case, but it does not seem to exist?

Text("SignIn.Body.Instruction".uppercased()) will uppercase the localization key, so that will not work, as instructed in this SO question about uppercasing a non-localized string.

like image 553
Sajjon Avatar asked Aug 20 '19 12:08

Sajjon


2 Answers

It is now possible with all new textCase ViewModifier (Xcode 12 Beta 3), like so:

Text("SignIn.Body.Instruction")
    .textCase(.uppercase)

Or lowercase:

Text("SignIn.Body.Instruction")
    .textCase(.lowercase)
like image 84
Sajjon Avatar answered Nov 14 '22 20:11

Sajjon


How about using the smallCaps modifier for Font? E.g., Text("Hello World").font(Font.body.smallCaps()) will render "HELLO WORLD", but the underlying string will retain its proper localized capitalization for the purposes of accessibility.

like image 20
marcprux Avatar answered Nov 14 '22 20:11

marcprux