UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

SOLVED: How do I make a String.StringInterpolation with NumberFormatter() return a Uppercase letter then lowercase letters

Forums > SwiftUI

I am trying to use the String.StringInterpolation extension with NumberFormatter() and .spellOut however this return the string as lowercase string (one, two etc) how would like to return string as uppercase for the the first letter then lowercase for the rest (One, Two etc) but only can add .uppercased() or .lowercased(). If anyone has any ideas?

Here some example code

struct ContentView: View {
    @State private var number = 1

    var body: some View {
        let numberString = "\(format: number, style: .spellOut)"

        return VStack {
            Text(numberString.uppercased())

            Stepper("Number", value: $number, in: 1...10)
            .padding()
        }

    }
}

extension String.StringInterpolation {
    mutating func appendInterpolation(format number: Int, style: NumberFormatter.Style) {
        let formatter = NumberFormatter()
        formatter.numberStyle = style

        if let result = formatter.string(from: number as NSNumber) {
            appendLiteral(result)
        }
    }
}

Appreciate yout help in advance

2      

Hi Nigel, This should solve your problem: Text(numberString.capitalized)

2      

Cheers. Could not find it anywhere in String documents

2      

TAKE YOUR SKILLS TO THE NEXT LEVEL If you like Hacking with Swift, you'll love Hacking with Swift+ – it's my premium service where you can learn advanced Swift and SwiftUI, functional programming, algorithms, and more. Plus it comes with stacks of benefits, including monthly live streams, downloadable projects, a 20% discount on all books, and free gifts!

Find out more

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.