Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create an uppercase version of a String in SwiftUI?

Tags:

swift

swiftui

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!")
like image 795
Sid Avatar asked Jul 09 '19 04:07

Sid


People also ask

Is string a Unicode character in Swift?

Despite this simplicity of syntax, Swift’s String type is a fast, modern string implementation. Every string is composed of encoding-independent Unicode characters, and provides support for accessing those characters in various Unicode representations. Swift’s String type is bridged with Foundation’s NSString class.

What are substrings in Swift and how to use them?

Substrings in Swift have most of the same methods as strings, which means you can work with substrings the same way you work with strings. However, unlike strings, you use substrings for only a short amount of time while performing actions on a string. When you’re ready to store the result for a longer time,...

How do you create a string in Swift?

The syntax for string creation and manipulation is lightweight and readable, with a string literal syntax that’s similar to C. String concatenation is as simple as combining two strings with the + operator, and string mutability is managed by choosing between a constant or a variable, just like any other value in Swift.

How do I find the prefix of a string in Swift?

String and character comparisons in Swift aren’t locale-sensitive. To check whether a string has a particular string prefix or suffix, call the string’s hasPrefix (_:) and hasSuffix (_:) methods, both of which take a single argument of type String and return a Boolean value.


2 Answers

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

Text(LocalizedStringKey("keyName"))
  .textCase(.uppercase)
like image 188
Maksymilian Tomczyk Avatar answered Oct 20 '22 05:10

Maksymilian Tomczyk


You can do this simply in this way

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

For LocalizedStringKey you can use this.

Text(LocalizedStringKey("keyName")).textCase(.uppercase)
like image 43
Faysal Ahmed Avatar answered Oct 20 '22 05:10

Faysal Ahmed