How to Create an Uppercase Version of a String in Swiftui

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

You can do this simply in this way

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

For LocalizedStringKey you can use this.

Text(LocalizedStringKey("keyName")).textCase(.uppercase)

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

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)

SwiftUI Font how to use uppercased() with LocalizedStringKey

Here is a solution. Tested with Xcode 11.4 / iOS 13.4

demo

Text(NSLocalizedString("HELLO_WORLD", comment: "").uppercased())
.font(.Heading1)

SwiftUI - Capitalise first letter

Any Swift (NS)String API can be used also in SwiftUI

Text("hello world".capitalized)

SwiftUI: search for lower and upper case letters

There is a dedicated API

return names.filter { $0.localizedCaseInsensitiveContains(searchText) }

Swift apply .uppercaseString to only the first letter of a string

Including mutating and non mutating versions that are consistent with API guidelines.

Swift 3:

extension String {
func capitalizingFirstLetter() -> String {
let first = String(characters.prefix(1)).capitalized
let other = String(characters.dropFirst())
return first + other
}

mutating func capitalizeFirstLetter() {
self = self.capitalizingFirstLetter()
}
}

Swift 4:

extension String {
func capitalizingFirstLetter() -> String {
return prefix(1).uppercased() + self.lowercased().dropFirst()
}

mutating func capitalizeFirstLetter() {
self = self.capitalizingFirstLetter()
}
}

How do I force a text field to be upper case only in Swift?

let textFieldOutput = "Wait a moment, please."
let newString = textFieldOutput.uppercased()
//The string is now "WAIT A MOMENT, PLEASE."

unable to keep normal case for text swift ui , getting converted to uppercase forcefully

not sure why but when I created new struct for the text and added the struct in the main view, the fonts worked fine.

dont know why it was getting overridden before. kind of like this.

if someone knows why this can happen, please lmk.

struct SingleCellHeaderView: View {

var text: String!

var body: some View {
Text(text).foregroundColor(.white)
.frame(height: 20, alignment: .leading)
.font(.custom("Maison Neue", size: 13))
.padding(.init(top: 12, leading: 20, bottom: 12, trailing: 12))
}
}

How to capitalize each word in a string using Swift iOS

Are you looking for capitalizedString

Discussion

A string with the first character in each word changed to its corresponding uppercase value, and all remaining characters set to their corresponding lowercase values.

and/or capitalizedStringWithLocale(_:)

Returns a capitalized representation of the receiver using the specified locale.

For strings presented to users, pass the current locale ([NSLocale currentLocale]). To use the system locale, pass nil.

Swift .uppercaseString or .lowercaseString property replacement

Xcode 6.0 / Swift 1.0

String is bridged seamlessly to NSString, so it does have uppercaseString and lowercaseString properties as long as you import Foundation (or really almost any framework since they'll usually import Foundation internally. From the Strings and Characters section of the Swift Programming Guide:

Swift’s String type is bridged seamlessly to Foundation’s NSString
class. If you are working with the Foundation framework in Cocoa or
Cocoa Touch, the entire NSString API is available to call on any
String value you create, in addition to the String features described
in this chapter. You can also use a String value with any API that
requires an NSString instance.


Xcode 6.1 / Swift 1.1

As @newacct pointed out, in Xcode 6.1 / Swift 1.1, uppercaseString and lowercaseString are in Swift's String class so you don't need to use the ones defined in NSString. However, it's implemented as an extension to the String class in the Foundation framework so the solution is still the same: import Foundation

In a playground:

import Foundation

var sillyString = "This is a string!" // --> This is a string!
let yellyString = sillyString.uppercaseString // --> THIS IS A STRING!
let silentString = sillyString.lowercaseString // --> this is a string!

Swift 3.0

In a playground:

import Foundation

var sillyString = "This is a string!" // --> This is a string!
let yellyString = sillyString.uppercased() // --> THIS IS A STRING!
let silentString = sillyString.lowercased() // --> this is a string!


Related Topics



Leave a reply



Submit