27

It is possible using Swift to create an uppercase version of a String with

let str = "Hello World!"
print(str.uppercased())

This code will print "HELLO WORLD!" into the Xcode console. But how do you create an uppercase of a String like the following using SwiftUI?

Text("Hello World!")
Sid
  • 963
  • 1
  • 12
  • 17

2 Answers2

40

For those who want to use LocalizedStringKey. Since iOS 14 you can use .textCase(.uppercase) on Text.

Text(LocalizedStringKey("keyName"))
  .textCase(.uppercase)
Maksymilian Tomczyk
  • 1,100
  • 8
  • 16
33

You can do this simply in this way

Text("Hello World!".uppercased())

For LocalizedStringKey you can use this.

Text(LocalizedStringKey("keyName")).textCase(.uppercase)
Faysal Ahmed
  • 7,501
  • 5
  • 28
  • 50
  • 20
    What about if you are using a LocalizedStringKey? – Daniel Wood Mar 18 '20 at 14:54
  • @Maksymilian Tomczyk answer is better since it operates on the view side not the data side, also it can be composed into a Style, so even if you're not LocalizedStringKey then it's still the way to do it. – Lucas van Dongen Jun 17 '21 at 13:21