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      

Hacking with Swift is sponsored by Essential Developer

SPONSORED Join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer! Hurry up because it'll be available only until April 28th.

Click to save your free spot now

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.